I am currently simulating some quantum circuits, and want to calculate the probabilities of observing each individual state. I am able to use Cirq for this, and calculate it using $P_{00} = |\alpha|^2$. Code:
import cirq
import sympy
x0, x1 = sympy.symbols('x0 x1')
q = cirq.GridQubit.rect(1, 2)
circuit = cirq.Circuit(
cirq.rx(x0).on(q[0]), cirq.rx(x1).on(q[1]),
cirq.ry(3.14/4).on(q[0]), cirq.ry(3.14/4).on(q[1]))
resolver = cirq.ParamResolver({x0: 0.2, x1: 0.3})
simulator = cirq.Simulator()
results = simulator.simulate(program=circuit, param_resolver=resolver,
qubit_order=q).final_state
print("Internal quantum state:", results)
print("Probabilities of observing each state:", [abs(x)**2 for x in results])
Output:
internal quantum state: [0.8377083+0.08743566j 0.3529709-0.11249491j 0.35297093-0.06251574j 0.13120411-0.08743566j]
probabilities of observing each state: [0.7094002059173512, 0.13724355108492148, 0.12849669756020887, 0.024859516013751914]
However, in multiple tutorials (for instance from TFQ) I see the use of "expectation_from_wavefunction":
z0 = cirq.Z(q[0])
qubit_map={q[0]: 1, q[1]: 1}
z0.expectation_from_wavefunction(results, qubit_map).real
output:
0.6757938265800476
My question:
How can I use expectation_from_wavefunction to obtain the probabilities of observing the individual states ($P_{00}, P_{01}, P_{10}, P_{11}$)?
Bonus question: why would I favor this approach?
expectation_from_wavefunction
is used when you don't want to write the logic for yourself. This is more useful in cases with multi-qubit observables involving the X and Y axies. $\endgroup$ – Craig Gidney Apr 21 '20 at 22:56