in the documentation for OpenQasm 2.0 it says one can use an if statement like so: if(c_reg==int) Quantum Operation.

However, I would like to execute the following: if(creg ==int) measure qreg -> creg;

I get no compilation errors in IBM Quantum Computing Learning Lab and am currently using an OpenQasm2.0 string input. The string is then turned into a circuit using the following: QuantumCircuit.from_qasm_str(program_str) and executed on the 'qasm_simlulator' backend using this command: execute(circuit,runtime_backend, shots=1024).

The problem I seem to have is that the measurement statement executes regardless of the if statement. Am I using the commands correctly?

Thanks for your time.

  • $\begingroup$ From the information you've given it seems like you are using the commands correctly. Depending on the probabilities associated with the conditional it could be that the circuit has a high likelihood of satisfying the "if". It could also be that whether the measurement is executed or not, the existing measurement counts aren't significantly impacted. Without seeing your circuit it's hard to say. It would be helpful to see some example code so I try to recreate the behavior. $\endgroup$
    – ryanhill1
    Aug 27, 2023 at 1:21

1 Answer 1


In the OpenQasm language, the if statement is typically used for conditional quantum operations, not for controlling classical operations like measurements. The if statement in OpenQasm allows you to conditionally apply quantum gates or operations based on the state of classical registers (cregs).

The correct usage of the if statement in OpenQasm 2.0 is as follows:

if(c_reg == int) QuantumOperation qreg;

Here, c_reg is the classical register you're checking, int is the integer value you're comparing it to, and QuantumOperation is the quantum operation you want to conditionally apply to qreg based on the condition.

However, you cannot use the if statement to conditionally execute classical operations like measurements (measure qreg -> creg). The if statement in OpenQasm is designed to conditionally apply quantum operations, not classical operations.

If you're experiencing an issue where the measurement statement is executing regardless of the if statement, there might be some other factors at play, such as the actual values of your classical registers, how they are being manipulated, or how the circuit is being executed. Without seeing your actual code and circuit, it's hard to pinpoint the exact issue.

To troubleshoot this, you might want to check the values stored in your classical registers (c_reg) and ensure that they match the conditions you are checking in your if statement. Additionally, you could try simplifying your code to a minimal example that still reproduces the issue and share that with the community or support forums for more specific assistance.

  • $\begingroup$ I don't know if this is entirely accurate... from arxiv.org/abs/1707.03429 last paragraph on page 7: "Only quantum operations, i.e. builtin gates, gate (and opaque) subroutines, preparation, and measurement, can be prefaced by if." So in the paper they include measurement in the list of operations that can be performed by "if"? $\endgroup$
    – ryanhill1
    Aug 27, 2023 at 16:16
  • $\begingroup$ @ryanhill1 Gotcha, You might check this thread out github.com/Qiskit/qiskit-aer/issues/268 It answers your concern and in short; You can rely on that arxiv.org/abs/1809.03452 as ref. for our context matter here. $\endgroup$ Aug 29, 2023 at 7:39

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.