Your mental model of how cirq works is slightly off. You don't invoke operations on a qubit in one line and then check how that qubit changed in another line. You create a circuit in one line and then ask about its properties, such as its effect on a qubit, in another line.
In this case, you want to ask what the final_wavefunction
of your circuit is and then trace out the other qubits. The output of final_wavefunction
is just a normal numpy array, which you can then analyze. To avoid a gotcha where qubits not operated on by the circuit are not included in the wavefunction, it's important to specify a qubit_order
for final_wavefunction
. Cirq also includes methods like wavefuntion_partial_trace_as_mixture
which can tell you about specific qubits or subsets of qubits from a wavefunction. The main annoyance there is that because all you have is a numpy array you need to talk in indices instead of in qubits.
Basically, do this:
import cirq
msg, R, S = cirq.LineQubit.range(3)
circuit = cirq.Circuit(cirq.H(R), cirq.CNOT(R, S))
full_wavefunction = cirq.final_wavefunction(
circuit,
qubit_order=[msg, R, S])
qubit_mixture = cirq.wavefunction_partial_trace_as_mixture(
full_wavefunction,
keep_indices=[1]) # offset 1 in qubit order [msg, R, S] is R
for probability, case in qubit_mixture:
print(f'{probability:%} {cirq.dirac_notation(case)}')
# prints:
# 49.999997% |0⟩
# 49.999997% |1⟩