# Grover (1/2)

**Catégorie:** Algorithmique Quantique - **Difficulté:** Facile

{% file src="/files/i7lXOAaSjG33Vcg1xoUe" %}

**Description:**

<figure><img src="/files/IyYQTvPdznvAJId5ICeL" alt=""><figcaption></figcaption></figure>

Solution:

Voici le script de résolution du challenge :&#x20;

{% code fullWidth="false" %}

```python
import json
import requests

from machinerie import Circuit, create_grover  # fonctions fournies :contentReference[oaicite:0]{index=0}:contentReference[oaicite:1]{index=1}


def build_f1() -> Circuit:
    """f₁(x) = ¬x (sur 1 qubit de sortie)."""
    qc = Circuit(2, name="f1")
    qc.cx(0, 1)   # copie x
    qc.x(1)       # NOT
    return qc


def build_f2() -> Circuit:
    """f₂( x₀ x₁ ) = ¬x₁ (sur un 3ᵉ qubit)."""
    qc = Circuit(3, name="f2")
    qc.cx(1, 2)   # copie x₁
    qc.x(2)       # NOT
    return qc


def build_grover() -> Circuit:
    """
    Oracle + diffuseur pour la cible ‘10’.
    flag = [0, 1]  ➜ qubit0 doit valoir 0, qubit1 doit valoir 1 :contentReference[oaicite:2]{index=2}:contentReference[oaicite:3]{index=3}
    """
    return create_grover(
        flag=[0, 1],          # cible = ‘10’ (q₁ = 1, q₀ = 0)
        hadamard_middle=[0, 1],
        hadamard_end=[0, 1],
    )


def solve_grover_challenge() -> None:
    f1 = build_f1()
    f2 = build_f2()
    grover = build_grover()

    # Test local : doit tout mettre sur |10>
    verifier = Circuit(2)
    verifier.h([0, 1])          # état uniforme
    verifier.compose(grover, inplace=True)
    print("Mesure locale (attendu ≈ {'10': 1.0}):",
          verifier.get_measure(shots=1024, normalise=True))

    # Matrices aplaties à envoyer
    payload = {
        "f1": f1.get_flat_unitary(),
        "f2": f2.get_flat_unitary(),
        "grover": grover.get_flat_unitary(),
    }

    url = "https://causapscal-des-profondeurs.404ctf.fr/challenges/2"
    headers = {"Content-Type": "application/json", "Accept": "application/json"}
    r = requests.post(url, json=payload, headers=headers)
    print("Réponse serveur :", r.json())


if __name__ == "__main__":
    solve_grover_challenge()
```

{% endcode %}

Et voilà le résultat :&#x20;

<figure><img src="/files/pmRQkgqfXPmb7hh87Kcu" alt=""><figcaption></figcaption></figure>

<details>

<summary>🚩FLAG</summary>

`404CTF{z0r_oU_XoR?_j3_N3_m_En_sOr_pLU5}`

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://writeups.ayweth20.com/2025/404ctf-2025/algorithmique-quantique/grover-1-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
