-
Notifications
You must be signed in to change notification settings - Fork 1
/
teleportation_circuit.py
154 lines (124 loc) · 4.44 KB
/
teleportation_circuit.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 1 10:14:27 2020
@collaborators: Hantoa, Derrick, Malvika, & Sergio
Quantum Teleportation from https://qiskit.org/textbook/ch-algorithms/teleportation.html
"""
import numpy as np
from qiskit import QuantumRegister, ClassicalRegister
from qiskit import(
QuantumCircuit,
execute,
Aer,
IBMQ)
from qiskit.providers.ibmq import least_busy
from qiskit.visualization import plot_histogram
import getpass
import matplotlib.pyplot as plt
# Create a Quantum Circuit acting on the q register
q = QuantumRegister(3, 'q')
c = ClassicalRegister(3, 'c')
circuit = QuantumCircuit(q, c)
# curQ is an int that is keeps track of which players qubit to modify
# and is 0 if it's player 1's turn to send the state 2 if it's player 2's turn.
scores = [0,0]
def getUserInput(curQ):
valid = "xyzht"
# print("Enter transformations to apply from (x,y,z,h,t)")
input_str = ""
while True:
check = True
choice = getpass.getpass("Enter transformations to apply from (x,y,z,h,t): ")
if len(choice) > 5 or len(choice) == 0 : check = False
for c in choice:
if c not in valid:
check = False
break
if check:
input_str = choice
break
else:
print("Enter a valid transformation")
#curQ>>1 gives which players turn it is (0/1)
scores[curQ>>1] += len(input_str)
return input_str
def player_operation(input_string, q, circuit, dagger):
apply_gate = {
'x': circuit.x,
'y': circuit.y,
'z': circuit.z,
'h': circuit.h,
't': circuit.t,
}
if dagger: apply_gate['t'] = circuit.tdg
if dagger:
[apply_gate[gate](q) for gate in input_string]
else:
[apply_gate[gate](q) for gate in input_string[::-1]]
# addTeleport adds a new teleportation to the circuit
def addTeleport(curQ, nxtQ):
# Design phi
#player_operation(getUserInput(), curQ, circuit, dagger=False)
circuit.barrier()
# Design beta_00
circuit.h(1)
circuit.cx(1, nxtQ)
circuit.barrier()
# First measurement on sender information
circuit.cx(curQ, 1)
circuit.h(curQ)
#circuit.measure(curQ, curQ)
#circuit.measure(1, 1)
# After sending classical information
circuit.cx(1, nxtQ)
circuit.cz(curQ, nxtQ)
circuit.barrier()
circuit.h(curQ)
circuit.h(1)
# endgame ends the game, does measurements, and draws the circuit.
def endgame(curQ, desired_device, measured=False):
# Apply transformation in reverse if we only care about input (currently 0?)
#player_operation(input_string, q[2], circuit, dagger=True)
# Measure q[2]
circuit.measure(curQ, curQ)
# Define device
if desired_device == 'sim':
device = Aer.get_backend('qasm_simulator')
shots = 1023
elif desired_device == 'qc':
print("Connecting to quantum computer...")
IBMQ.save_account('292ebd1c42498b47d4d3c1076b3afa016395350f214fcc5d598241639624171f63a78ddde5ae15d23445c9627c7da5e8883c42d020c4cf84451dc39e36a6c6cb', overwrite=True)
provider = IBMQ.load_account()
device = least_busy(provider.backends(simulator=False))
shots = 127
print("Making quantum computations...")
# Execute the circuit on the qasm simulator or device
job = execute(circuit, backend=device, shots=shots)
# Grab results from the job
result = job.result()
# Returns counts
counts = result.get_counts(circuit)
print("\nTotal count are:",counts)
if(measured):
player = (curQ>>1)+1
measurementEnd(counts, player, shots>>1)
print("Number of gates applied by each player ",scores)
circuit.draw(output='mpl').show()
plt.show()
def measurementEnd(counts, player, half):
winCount = 0
for x in range(4,8):
winState = bin(x)[2::]
if (player == 1):
winState = winState[::-1]
if(winState in counts):
winCount = winCount + counts[winState]
if(winCount > half):
print("More than half of the measurements gave 1!")
print("Player " + str(player) + " wins")
else:
print("Less than half of the measurements gave 1 :(")
print("Player " + str(((player-1)^1)+1) + " wins")
# Draw the circuit in Console separately!!!
#circuit.draw(output='mpl')
#plot_histogram(counts)