3
$\begingroup$

I am trying to calculate the expectation value of a customized operator on two similar but differently constructed states $\phi = \phi_2 =\frac{1}{\sqrt{2}}\left( |001⟩ + |011⟩\right)$ which consist of more qubits than the operator. (via Qiskit)

import numpy as np
from qiskit import QuantumCircuit
from qiskit.aqua.operators import CircuitOp, state_fns, Zero, One

circuit = QuantumCircuit(2)
circuit.z(0)
circuit.z(1)

op = CircuitOp(circuit)

psi = QuantumCircuit(3)
psi.h(1)
psi.x(2)
psi = state_fns.CircuitStateFn(circuit)
psi2 = 1 / np.sqrt(2) * ((Zero ^ Zero ^ One) + (Zero ^ One ^ One))


print('Expectation for phi:', psi.adjoint().compose(op).compose(psi).eval().real)  # Expectation for phi: 1.0
print('Expectation for phi2:', psi2.adjoint().compose(op).compose(psi2).eval().real) # Error

The second expectation raises an error: ValueError: Composition is not defined over Operators of different dimensions, 3 and 2, respectively.

Why is this happening? Why the first expectation is executed fine but not the second one?

$\endgroup$
5
$\begingroup$

First of all, I think there is a little mistake in the way you define psi. Indeed, you first create it as a 3-qubits circuit and then "erase" this by doing psi = state_fns.CircuitStateFn(circuit), meaning it will be exactly the same as the operator you defined right before, op. You can check the circuit you create via an operator using for example psi.primitive.draw() if you want to see! This is why your code worked for psi but not for psi2.

Then, Qiskit won't do the composition of operators that don't have the same size because it is not mathematically possible with matrices. May I suggest adding a third qubit on op without adding any gates on it, this way op will have the same size as psi and psi2 and your code will run without errors since all the operators will have the same size!
I hope this will help you :)

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.