# Cirq - Print state of qubit

Seeing a state of qubit is too complicated in Cirq I don't want to simulate, I would be happy for debugging, to see the probability/ amplitude of the state of a qubit, without measure and simulate

Something like:

circuit = cirq.Circuit()
msg, R, S = cirq.LineQubit.range(3)

circuit.append([cirq.H(R),cirq.CNOT(R, S)])

print(R.state)
"R = 0.12|0>+0.58|1>"
or
"(0.12,0.58)


Is there anyway to do this?

• Is it possible to get the state of a qubit that's not an initial state without simulating the circuit? – Victory Omole Mar 14 '20 at 14:24

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⟩