-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame2.py
More file actions
63 lines (51 loc) · 1.89 KB
/
game2.py
File metadata and controls
63 lines (51 loc) · 1.89 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
56
57
58
59
60
61
62
63
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
def run_quantum_level(qubits, target_room, player_moves):
"""
qubits: Number of qubits (2 or 3)
target_room: The binary string to find (e.g., "101")
player_moves: List of gates (e.g., ["X1", "CCZ", "X1"])
"""
qc = QuantumCircuit(qubits)
# 1. INITIAL SUPERPOSITION (The 'Start' of the maze)
qc.h(range(qubits))
qc.barrier()
# 2. THE PLAYER'S ORACLE (The 'Lock')
# Goal: Flip the sign of the target_room
for gate in player_moves:
if gate == 'X0': qc.x(0)
if gate == 'X1': qc.x(1)
if gate == 'X2': qc.x(2)
# Select the right 'Lock' gate for the level
if gate == 'CZ' and qubits == 2: qc.cz(0, 1)
if gate == 'CCZ' and qubits == 3: qc.ccz(0, 1, 2)
qc.barrier()
# 3. THE DIFFUSER (The 'Amplifier')
# This is the universal part that makes the marked room 'bright'
qc.h(range(qubits))
qc.x(range(qubits))
if qubits == 2:
qc.cz(0, 1)
else:
qc.h(qubits-1)
qc.mcx(list(range(qubits-1)), qubits-1)
qc.h(qubits-1)
qc.x(range(qubits))
qc.h(range(qubits))
# 4. MEASURING THE OUTCOME
state = Statevector.from_instruction(qc)
probs = state.probabilities_dict()
# In Qiskit, we need to handle the bit-string ordering correctly
# Reversed because Qiskit uses Little Endian (q2 q1 q0)
win_prob = probs.get(target_room[::-1], 0)
return win_prob, probs
# --- THE GAME LEVELS ---
# LEVEL 1: Target 11 (No X gates needed)
# Correct: ['CZ']
prob1, _ = run_quantum_level(2, "11", ["CZ"])
# LEVEL 2: Target 01 (Must flip Qubit 1)
# Correct: ['X1', 'CZ', 'X1']
prob2, _ = run_quantum_level(2, "01", ["X1", "CZ", "X1"])
# LEVEL 3: Target 101 (Must flip Qubit 1)
# Correct: ['X1', 'CCZ', 'X1']
prob3, _ = run_quantum_level(3, "101", ["X1", "CCZ", "X1"])