I have to compute $\langle\psi_1(\theta)|H_1|\psi_1(\theta)\rangle$

where $$H_1 = X_1Z_2 +3Y_0Y_4$$

psi1 = RealAmplitudes(5, reps=2)
H1 = SparsePauliOp.from_list([("XZ", 1),("YY",3)])
theta1 = np.linspace(0,1,5*3)

with Session(service=service, backend=backend):
    options = Options(simulator={"seed_simulator": 42}, resilience_level=0)
    estimator = Estimator(options=options)
    job = estimator.run(circuits=[psi1], 
    result = job.result()

Though, I'm getting the below error:

Traceback (most recent call last):
  Input In [52] in <cell line: 3>
    job = estimator.run(circuits=[psi1],
  File /opt/conda/lib/python3.8/site-packages/qiskit_ibm_runtime/estimator.py:249 in run
    return super().run(
  File /opt/conda/lib/python3.8/site-packages/qiskit/primitives/base/base_estimator.py:227 in run
    self._cross_validate_circuits_observables(circuits, observables)
  File /opt/conda/lib/python3.8/site-packages/qiskit/primitives/base/base_estimator.py:283 in _cross_validate_circuits_observables
    raise ValueError(
ValueError: The number of qubits of the 0-th circuit (5) does not match the number of qubits of the 0-th observable (2).

Can anyone tell me what actually is causing this issue and how can this be solved?


1 Answer 1


The way you're using SparsePauliOp.from_list is wrong since it doesn't specify that there are $5$ qubits in your circuit. Instead, you should use it like this:

H1 = SparsePauliOp.from_list([("IIZXI", 1),("YIIIY",3)])

Because Qiskit uses little-endian notation the qubits are ordered from right to left.

Alternatively, you can utilize the method SparsePauliOp.from_sparse_list as shown here.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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