-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgate1.py
More file actions
55 lines (43 loc) · 1.63 KB
/
gate1.py
File metadata and controls
55 lines (43 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
def play_level(target, player_oracle_moves):
qc = QuantumCircuit(2)
qc.h([0, 1])
# --- STEP 1: PLAYER'S ORACLE ---
for gate in player_oracle_moves:
if gate == 'X0': qc.x(0)
if gate == 'X1': qc.x(1)
if gate == 'CZ': qc.cz(0, 1)
# --- CRITICAL RULE: UN-COMPUTATION ---
# If the user doesn't un-flip the X gates here,
# the Diffuser will amplify the wrong state.
# --- STEP 2: THE STANDARD DIFFUSER ---
qc.h([0, 1])
qc.x([0, 1])
qc.h(1)
qc.cx(0, 1)
qc.h(1)
qc.x([0, 1])
qc.h([0, 1])
# --- STEP 3: RESULTS ---
state = Statevector.from_instruction(qc)
probs = state.probabilities_dict()
win_prob = probs.get(target, 0)
print(f"\nTesting Target {target} with moves {player_oracle_moves}")
print(f"Probability of Target: {win_prob*100:.1f}%")
# A successful Grover search must leave the qubits in the correct orientation
if win_prob > 0.9:
print("🏆 SUCCESS!")
else:
# Find what they actually found
actual_find = max(probs, key=probs.get)
print(f"💀 FAIL! You marked the wrong state or forgot to un-flip your X gates!")
# --- START THE GAME ---
# This should FAIL because of the extra X0
play_level("11", ["X0", "CZ"])
# This should PASS because 11 needs no flipping
play_level("11", ["CZ"])
# This should FAIL because 01 needs an X1 to be seen by the CZ
play_level("01", ["CZ"])
# This should PASS because it flips the 0 to a 1, marks it, and flips it back
play_level("01", ["X1", "CZ", "X1"])