In [1]:
from qiskit import *
from qiskit.visualization import plot_histogram
%matplotlib inline
In [2]:
circuit = QuantumCircuit(3,3)
circuit.x(0) #Transfer to 1 state - NOT gate
circuit.barrier() #Barrier to make the circuit look better
circuit.h(1) # Apply H gate to q1
circuit.cx(1,2) # Apply CNOT to entangle q1 and q2
circuit.barrier()
circuit.draw(output='mpl')
Out[2]:
In [3]:
# Then we need to apply CNOT to q0 and q1, then Hadamard to q0
circuit.cx(0,1)
circuit.h(0)
circuit.barrier()
circuit.draw(output='mpl')
Out[3]:
In [4]:
# After this, Alice can measure the qubits. Depending on the situation, Bob can make adjustments
circuit.measure([0,1],[0,1])
circuit.barrier()
circuit.draw(output='mpl')
Out[4]:
In [5]:
# Depending on the situation, Bob will apply I, X, Z or XZ gates
# That situation depends on the values of q0 and q1
# Now, apply CNOT gate to q1 and q2, then CZ to q0 and q2
circuit.cx(1,2)
circuit.cz(0,2)
circuit.measure([2],[2])
circuit.draw(output='mpl')
Out[5]:
In [6]:
from qiskit_aer import Aer
simulator = Aer.get_backend("qasm_simulator")
result = simulator.run(circuit, shots=1024).result()
plot_histogram(result.get_counts(circuit))
Out[6]: