Grover (1/2)
Last updated
Was this helpful?
Last updated
Was this helpful?
Catégorie: Algorithmique Quantique - Difficulté: Facile
Description:
Solution:
Voici le script de résolution du challenge :
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()
Et voilà le résultat :