How to take classical input in a Cirq circuit

474 Views Asked by At

I'm new to quantum computing and have been playing with Cirq as I read through Quantum Computation and Quantum Information by Nielsen and Chuang. One of the first interesting circuits in the text relates to quantum teleportation and I am attempting to implement it in Cirq. Constructing the EPR pair and most of the circuit as described is pretty straightforward.

However, Bob needs to "fix up" his EPR qubit based on measurements from both Alice's EPR qubit and her other qubit (labeled ψ in the text). It's unclear to me the best way to incorporate the classical bits from the measurement into the circuit.

What I've done so far is to build the circuit without the fix up, run a simulation and then append the appropriate X and/or Z gates based on the the values measured. The code looks like

import cirq

psi_qubit = cirq.GridQubit(0, 0)
epr_alice = cirq.GridQubit(0, 1)
epr_bob = cirq.GridQubit(0, 2)

circuit = cirq.Circuit()

# Generate the EPR pair
circuit.append(
    [
        cirq.H(epr_alice),
        cirq.CNOT(epr_alice, epr_bob)
    ]
)

# Create the teleporation circuit without the fix up on Bob's EPR pair
circuit.append(
    [
        cirq.CNOT(psi_qubit, epr_alice),
        cirq.H(psi_qubit),
        cirq.measure(psi_qubit, epr_alice)
    ]
)

simulator = cirq.Simulator()
result = simulator.run(circuit)

measurement = result.measurements[f"{psi_qubit},{epr_alice}"][0]

if measurement[1]:
    circuit.append([cirq.X(epr_bob)])
if measurement[0]:
    circuit.append([cirq.Z(epr_bob)])
circuit.append(cirq.measure(epr_bob))

print(circuit)

The printed circuit looks like

(0, 0): ───────────@───H───M───────────
                   │       │
(0, 1): ───H───@───X───────M───────────
               │
(0, 2): ───────X───────────────Z───M───

While this circuit agrees with what's printed in the text for the case when Alice's EPR qubit measures |0> and her other qubit measures |1>, running a simulation again with the complete circuit is not guaranteed to yield the same measurements for Alice's qubits and thus the fix up portion will be incorrect.

What's the best way to feed the values from measurement back into the circuit?

1

There are 1 best solutions below

0
On

Cirq intentionally does not have this feature yet, because most hardware doesn't have this feature and one of Cirq's design goals is to be driven by hardware capabilities.

You can work around the omission by inserting CNOT and CZ operations with their control on the measured qubits, and just pretending they're classically controlled.