Skip to content

Instantly share code, notes, and snippets.

@GDLMadushanka
Created December 12, 2020 15:21
Show Gist options
  • Save GDLMadushanka/68dbdf932747a8e6c6b4343029d07e86 to your computer and use it in GitHub Desktop.
Save GDLMadushanka/68dbdf932747a8e6c6b4343029d07e86 to your computer and use it in GitHub Desktop.
Quantum teleportation algorithm using Qiskit
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
from qiskit.visualization import plot_bloch_multivector
from math import pi
%matplotlib inline
# We need three qubits and two classical bits for the algorithm.
q = QuantumRegister(3,"q")
# We have to define classical registers one by one since
# we are applying the C_if gate later
c1 = ClassicalRegister(1,"c1")
c2 = ClassicalRegister(1,"c2")
qc = QuantumCircuit(q,c1,c2)
# Crating the angle pi/4 = 45 degrees
theta = pi*(1/4)
# Rotate the teleporting qubit
qc.ry(theta,q[2])
backend = Aer.get_backend('statevector_simulator')
result = execute(qc, backend).result()
psi = result.get_statevector()
print("States of the three qubits before teleporting")
# Displaying the three qubits in bloch sphere
display(plot_bloch_multivector(psi))
# Creating the bell state
qc.h(q[1])
qc.cx(q[1],q[0])
# CNOT gate - teleporting qubit as the control bit
qc.cx(q[2],q[1])
# Hadamard gate on the teleporting qubit
qc.h(q[2])
qc.barrier()
# Alice measure her qubits
qc.measure(q[2],c2)
qc.measure(q[1],c1)
qc.barrier()
# Applying the classical if
# If classical bit is set apply the gate
qc.x(q[0]).c_if(c1,1)
qc.z(q[0]).c_if(c2,1)
# draw the circuit
display(qc.draw(output='mpl',reverse_bits=True))
backend = Aer.get_backend('statevector_simulator')
result = execute(qc, backend).result()
psi = result.get_statevector()
print("States of the three qubits after teleporting")
# Displaying the three qubits in bloch sphere
display(plot_bloch_multivector(psi))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment