# Best way to compute $\langle a|B|a \rangle$ in Cirq, where a is a state obtained running circuit A. And B is a different Quantum Circuit

I am implementing RQAOA in Cirq. After running regular QAOA to find an optimal state a (This I have done successfully).

I need to calculate $$\langle a|Z_iZ_j|a\rangle$$ for all $$i,j$$ in MyGraph.edges().

How should I go about using state a found with the QAOA circuit, to calculate the expectation value of a different circuit with that state?

• Welcome! Could you add some relevant links/code upon which others can build up upon? Mar 15, 2021 at 11:31

If you've already simulated the final state $$|a\rangle$$, something like the following should work:

qubits = cirq.LineQubit.range(nqubits)

# qubit order in the observables must match the qubit order in the circuit used to generate |a>
qubit_map = dict(zip(qubits, range(nqubits)))

for (i, j) in MyGraph.edges():
# make the Z_i*Z_j observable
ZiZj = cirq.Z(qubits[i]) * cirq.Z(qubits[j])
# compute desired expectation
expectation_ZiZj = ZiZj.expectation_from_state_vector(a, qubit_map=qubit_map)


Also in the expression $$\langle a | B | a \rangle$$, $$B$$ is generally not a quantum circuit, it needs to be an "observable" (Hermitian operator).

• Great that is exactly what I needed, thanks @forky40!! Mar 16, 2021 at 8:54