Felix Ha
Felix Ha

Reputation: 411

Implement quantum teleportation in qiskit

I am trying to implement the quantum teleportation protocol from the qiskit textbook in qiskit: I start with q_0 bit = 1 and I expect that q_3 = 1 at the end but it does not work.

from qiskit import *

qc = QuantumCircuit(3, 3)

qc.x(0) #q -> 1
qc.barrier()

qc.h(1)
qc.cx(1, 2)
qc.barrier()
# Next, apply the teleportation protocol.
qc.cx(0, 1)
qc.h(0)
qc.barrier()
# We measure these qubits and use the classical results to perform an operation
qc.measure(0, 0)
qc.measure(1, 1)
qc.cx(1, 2)
qc.cz(0, 2)
#qc.barrier()
backend = Aer.get_backend('qasm_simulator')
job = execute(qc, backend, shots=1, memory=True).result()
result = job.get_memory()[0]
qc.measure(2, 2)
print(job.get_memory()[0]) #q = 0

Upvotes: 2

Views: 625

Answers (3)

Matteo Paltenghi
Matteo Paltenghi

Reputation: 1

I believe the problem is with the qc.measure(2, 2) which is performed after the execution of the circuit job = execute(qc, backend, shots=1, memory=True).result(). Everything added after the execute is ignored because you run the circuit at the execute statement.

To fix it you can anticipate the measurement before the execution:

from qiskit import *

qc = QuantumCircuit(3, 3)

qc.x(0) #q -> 1
qc.barrier()

qc.h(1)
qc.cx(1, 2)
qc.barrier()
# Next, apply the teleportation protocol.
qc.cx(0, 1)
qc.h(0)
qc.barrier()
# We measure these qubits and use the classical results to perform an operation
qc.measure(0, 0)
qc.measure(1, 1)
qc.cx(1, 2)
qc.cz(0, 2)
#qc.barrier()
qc.measure(2, 2)  # <--- anticipated
backend = Aer.get_backend('qasm_simulator')
job = execute(qc, backend, shots=1, memory=True).result()
result = job.get_memory()[0]
print(job.get_memory()[0]) #q = 0

I hope it helps. Cheers

Upvotes: 0

April
April

Reputation: 1

enter image description here

From your code, I have got the circuit. I think the reason that can not get q_3 rightly. Is " Qubit measurement is followed by instructions".After the measurement, you can't do operations anymore. But I'm not sure this is the reason or not.

Upvotes: 0

Matthew Stypulkoski
Matthew Stypulkoski

Reputation: 251

It looks like it is working as intended. In the textbook, it says towards the end of the code cell:

In principle, if the teleportation protocol worked, we have q[2] = secret_unitary|0> As a result, we should be able to recover q[2] = |0> by applying the reverse of secret_unitary since for a unitary u, u^dagger u = I.

You had your secret_unitary as 'x', which does in fact change Alice's first qubit to 1. But, at the end of the circuit, the dagger of the secret_unitary is applied, cancelling the original application of the secret_unitary. You should expect to see 0 for q[2], as that means that the state from q[0] (in this case, 1) was successfully teleported to q[2], and then brought back to 0 by the dagger of the secret_unitary.

Upvotes: 1

Related Questions