-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbell-inequality-test.py
129 lines (104 loc) · 3.57 KB
/
bell-inequality-test.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import numpy as np
import cirq
def main():
circuit=make_bell_test_circuit()
print("Circuit:")
print(circuit)
print()
repetititons=1000
print('Simulating {} repetitions...'.format(repetitions))
result=cirq.Simulator().run(program=circuit,repetitions=repetitions)
a=np.array(result.measurements['a'][:, 0])
b=np.array(result.measurements['b'][:, 0])
x=np.array(result.measurements['x'][:, 0])
y=np.array(result.measurements['y'][:, 0])
outcomes=a^b==x&y
win_percenta=len([e for e in outcomes if e])*100/repetititons
print()
print('Results')
print('a:',bitstring(a))
print('b:',bitstring(b))
print('x:',bitstring(x))
print('y:',bitstring(y))
print('(a XOR b)==(x AND y):\n',bitstring(outcomes))
print('Win rate: {}%'.format(win_percent))
def make_bell_test_circuit():
alice=cirq.GridQubit(0,0)
bob=cirq.GridQubit(1,0)
alice_referee=cirq.GridQubit(0,1)
bob_referee=cirq.GridQubit(1,1)
circuit=cirq.Circuit()
circuit.apppend([cirq.H(alice),cirq.CNOT(alice,bob),cirq.X(alice)**-0.25,])
circuit.append([cirq.H(alice_referee),cirq.H(bob_referee),])
circuit.append([cirq.CNOT(alice_referee,alice)**0.5,cirq.CNOT(bob_referee,bob)*0.5,])
circuit.append([cirq.measure(alice,key='a'),cirq.measure(bob,key='b'),cirq.measure(alice_referee,key='x'),cirq.measure(bob_referee,key='y')])
return circuit
def bitstring(bits):
return ''.join('1' if e else '_' for e in bits)
"""Creates and simulates a circuit equivalent to a Bell inequality test."""
# Imports
import numpy as np
import cirq
def main():
# Create circuit
circuit = make_bell_test_circuit()
print('Circuit:')
print(circuit)
# Run simulations
print()
repetitions = 1000
print('Simulating {} repetitions...'.format(repetitions))
result = cirq.Simulator().run(program=circuit,
repetitions=repetitions)
# Collect results
a = np.array(result.measurements['a'][:, 0])
b = np.array(result.measurements['b'][:, 0])
x = np.array(result.measurements['x'][:, 0])
y = np.array(result.measurements['y'][:, 0])
# Compute the winning percentage
outcomes = a ^ b == x & y
win_percent = len([e for e in outcomes if e]) * 100 / repetitions
# Print data
print()
print('Results')
print('a:', bitstring(a))
print('b:', bitstring(b))
print('x:', bitstring(x))
print('y:', bitstring(y))
print('(a XOR b) == (x AND y):\n ', bitstring(outcomes))
print('Win rate: {}%'.format(win_percent))
def make_bell_test_circuit():
# Qubits for Alice, Bob, and referees
alice = cirq.GridQubit(0, 0)
bob = cirq.GridQubit(1, 0)
alice_referee = cirq.GridQubit(0, 1)
bob_referee = cirq.GridQubit(1, 1)
circuit = cirq.Circuit()
# Prepare shared entangled state between Alice and Bob
circuit.append([
cirq.H(alice),
cirq.CNOT(alice, bob),
cirq.X(alice)**-0.25,
])
# Referees flip coins
circuit.append([
cirq.H(alice_referee),
cirq.H(bob_referee),
])
# Players do a sqrt(X) based on their referee's coin
circuit.append([
cirq.CNOT(alice_referee, alice)**0.5,
cirq.CNOT(bob_referee, bob)**0.5,
])
# Then results are recorded
circuit.append([
cirq.measure(alice, key='a'),
cirq.measure(bob, key='b'),
cirq.measure(alice_referee, key='x'),
cirq.measure(bob_referee, key='y'),
])
return circuit
def bitstring(bits):
return ''.join('1' if e else '_' for e in bits)
if __name__ == '__main__':
main()