I've been trying to implement a QML algorithm but I've run into a problem with calculating the expectation value of a certain part of the circuit after doing mid circuit measurements. I have a simplified example for you:

Let's say I have a circuit prepared in some state and some observable:

X = Operator([[0, 1], [1, 0]])
M_hat = X.tensor(X).tensor(X).tensor(X).tensor(X).tensor(X)

psi = QuantumCircuit(6, 2)

Then, I do some operations of some of the qubits and then measure them:


psi.measure(0, 0)
psi.measure(1, 1)


And finally I try to calculate the expval:

estimator = Estimator(options={"shots" : 1})
expectation_value = estimator.run(psi, M_hat, run_options={"shots" : 1}).result().values
print("expectation: ", expectation_value)

I get and error saying: "qiskit.exceptions.QiskitError: 'Cannot apply instruction with classical bits: measure'"

Is there any way to delete the qubit that I don't need on the circuit anymore or calculate the expectation value without the measured qubits?


1 Answer 1


The error you're encountering is because the Qiskit backend isn't able to compute the expectation value of an operator on a circuit that contains classical measurement instructions.

To compute the expectation value after the measurements, you'd typically have to:

  1. Run the circuit for multiple shots
  2. For each shot, obtain the measurement result and calculate the expectation value
  3. Average the results of the expectation values over all shots

Here's how this could look:

# Create the Observable using SparsePauliOp
X = SparsePauliOp(Pauli('X'))
M_hat = X.tensor(X).tensor(X).tensor(X).tensor(X).tensor(X)
matrix_m_hat = np.real(M_hat.to_matrix())

n = 6
# Quantum circuit
psi = QuantumCircuit(n)

# Use Aer's qasm_simulator
backend = Aer.get_backend('qasm_simulator')
t_qc = transpile(psi, backend=backend)
result = backend.run(t_qc, shots=1000).result()
counts = result.get_counts()

for key, value in counts.items():
    # Convert bit string to state vector
    idx = int(key, 2)  # Convert binary string to integer
    state_vector = np.zeros(2**n)
    state_vector[idx] = 1

    # Calculate expectation value for this outcome
    outcome_exp_value = statevector.T.conj() @ matrix_m_hat @ statevector
    expectation_value += outcome_exp_value * value

expectation_value /= 1000  # Divide by total number of shots to get the average

print(f"Expectation Value: {np.round(expectation_value,2).real}")

As an alternative, if you are just simulating, you could use a statevector simulator to compute the expectation value directly. With this, you don't have to go through the process of measuring, counting, and then estimating the expectation values. This could look something like:

# Create the Observable using SparsePauliOp
X = SparsePauliOp(Pauli('X'))
M_hat = X.tensor(X).tensor(X).tensor(X).tensor(X).tensor(X)
matrix_m_hat = np.real(M_hat.to_matrix())

# Circuit
psi = QuantumCircuit(6)

# No measurements or resets needed for statevector simulation
simulator = Aer.get_backend('statevector_simulator')
result = execute(psi, simulator).result()
statevector = np.array(result.get_statevector())

# Compute the expectation value
expectation = statevector.T.conj() @ matrix_m_hat @ statevector

print("expectation: ", expectation.real)

Note that this might not be the most efficient implementation, but is a good illustration of the idea.

  • $\begingroup$ Thank you for your help! The idea is that the two measured qubits here are ancilla qubits which can be discarded. I'd like to someday run this algorithm on a real device so I'd not like to go with the statevector approach. If I were to run the rest of the circuit with multiple shots and obtain the measurement results, how would I calculated the expectation value from the obtained samples? $\endgroup$ Sep 21 at 15:06
  • $\begingroup$ No problem @FransPerkkola. I've edited in an example measuring all the qubits for multiple shots. Again, this is a pretty crude implementation that illustrates the general idea but I hope this goes some way to helping. $\endgroup$
    – banercat
    Sep 21 at 15:48
  • $\begingroup$ Yes, this helps a lot. Thank you! $\endgroup$ Sep 21 at 15:53

Your Answer

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

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