Skip to content

Instantly share code, notes, and snippets.

@silgon
Created October 13, 2023 20:11
Show Gist options
  • Save silgon/d055a3d085dd34d3cbc2debe23a83f0d to your computer and use it in GitHub Desktop.
Save silgon/d055a3d085dd34d3cbc2debe23a83f0d to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
from qiskit import *
from qiskit.tools.monitor import job_monitor
from qiskit.tools.visualization import plot_bloch_multivector, plot_histogram
from math import pi
qr = QuantumRegister(3, name="q")
crz, crx = ClassicalRegister(1, name="crz"), ClassicalRegister(1, name="crx")
c0 = ClassicalRegister(1, name="c0")
qc = QuantumCircuit(qr, crz, crx, c0)
θ = pi/2
θ = 0
θ = pi
qc.u(θ,0,0,0)
# bell state (qubit 1: alice, qubit 2: bob)
qc.h(1)
qc.cx(1, 2)
qc.barrier()
# measurement for teleportation
qc.cx(0,1)
qc.h(0)
qc.barrier()
qc.measure([0, 1],[0, 1])
qc.x(2).c_if(crx, 1) # Apply gates if the registers
qc.z(2).c_if(crz, 1) # are in the state '1'
qc.measure(2,2)
simulator = Aer.get_backend("qasm_simulator")
sim_result = execute(qc, backend=simulator, shots=1024).result()
plot_histogram(sim_result.get_counts())
import matplotlib.pyplot as plt
from qiskit import *
from qiskit.tools.monitor import job_monitor
from qiskit.tools.visualization import plot_bloch_multivector, plot_histogram
from math import pi
n = 7
qcs = []
for i in range(n):
qr = QuantumRegister(3, name="q")
cr = ClassicalRegister(3, name="c")
qc = QuantumCircuit(qr, cr)
θ = pi*i/(n-1)
qc.u(θ,0,0,0)
# bell state
qc.h(1)
qc.cx(1, 2)
qc.barrier()
# measurement for teleportation
qc.cx(0,1)
qc.h(0)
qc.barrier()
qc.cx(1, 2)
qc.cz(0, 2)
qc.measure_all()
qcs.append(qc)
IBMQ.load_account()
provider = IBMQ.get_provider("ibm-q")
qcomp = provider.get_backend("ibm_perth")
job = execute(qcs, backend=qcomp)
real_result = job.result()
q2s = [{"0":0,"1":0} for _ in range(n)]
for i in range(n):
tmp = real_result.get_counts(qcs[i])
for k,v in tmp.items():
q2s[i][k[0]]+=v/4000
import matplotlib.pyplot as plt
from qiskit import *
from qiskit.tools.monitor import job_monitor
from qiskit.tools.visualization import plot_bloch_multivector, plot_histogram
from math import pi
n = 7
qcs = []
for i in range(n):
qr = QuantumRegister(3, name="q")
qc = QuantumCircuit(qr)
θ = pi*i/(n-1)
qc.u(θ,0,0,0)
# bell state
qc.h(1)
qc.cx(1, 2)
# NEW next line to have the |01>+|10> state
qc.x(1)
qc.barrier()
# measurement for teleportation
qc.cx(0,1)
qc.h(0)
qc.barrier()
# NEW nex line to control as needed
qc.x(1)
qc.cx(1, 2)
qc.cz(0, 2)
qc.measure_all()
qcs.append(qc)
IBMQ.load_account()
provider = IBMQ.get_provider("ibm-q")
qcomp = provider.get_backend("ibm_perth")
job = execute(qcs, backend=qcomp)
real_result = job.result()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment