Branch Optimization
We optimize the conditional branching statements using the following techniques:- Branch Elimination: We evaluate the outcome of the branch condition, if it is known at compile time and does not contain measurement results of qubits. We remove the branch statement and if it evaluates to a true value, attach the corresponding block of code to the main program.
- Branch Unfolding: If the branch condition contains measurement results of qubits, we unfold the classical registers into individual bits and insert equivalent conditional statements for each bit. This method is particularly useful for systems which do not support multi-bit classical registers in conditional statements.
Switch Case Optimization
The openqasm reference enforces the switch targets to be of type int and the cases to be unique integer literals or constant integer expressions. This implies that the switch case statements can be optimized at compile time as long as the target variable is not dependent on a measurement result. Once the target variable is evaluated at compile time, theswitch
statement is removed and the
corresponding switch
case code block is attached to the main program.
Subroutine Inlining
Subroutine inlining is the process of replacing a subroutine call with the actual code of the subroutine. Since quantum devices do not support subroutines, it is essential to inline the subroutine code against the call to the subroutine. Here, the formal parameters of the subroutine are replaced with the actual parameters used in the subroutine call. Moreover, the subroutine code is inserted at the location of the subroutine call with careful consideration of the scope of the variables used in the subroutine.Loop Unrolling
Loop unrolling is the process of unfolding a loop with a list of equivalent instructions. This is particularly important for quantum programs as quantum computers do not support direct execution of loops. We provide support for unrollingwhile
and for
loops -
while
Loop- We evaluate the loop conditions at compile time and visit the loop body till the condition becomes false.
- To ensure that the unrolling process does not go into an infinite
loop, we have introduced a
max_loop_iters
parameter. It has a default value of1e9
and allows the user to control the max number of loop iterations -
- This unrolling process is not applicable in a case when the
while
condition is dependent on a quantum measurement result. Since the truth value of the condition can only be known at runtime, an exception is raised while unrolling such loops -
while
loops which contain quantum measurements is, in fact, an active area of development.
Some researchers have proposed to identify such loops and convert them into a native while
loop instructions which can be executed during runtime. However, this approach has not been
fully implemented yet.
for
Loop- The process is similar to the
while
loop unrolling process, but the loop counter is chosen from the range provided by the user. The loop body is executed as many times as the range value -
- The process is similar to the
Qubit Register Consolidation
This feature enables the transformation of multiple named qubit registers into a single unified register named__PYQASM_QUBITS__
. This simplifies register management and prepares the circuit for hardware-specific or simulator-specific backends that expect a flat register structure.
Usage
-
If
device_qubits
is provided while loading the QASM file, enforces a qubit limit and validates register size. - If not provided, the unified register size equals the total number of qubits across all original registers.
- Updates all qubit references in the code to align with the new unified register.
Circuit Timing Operations
In OpenQASM 3, circuit execution is not only determined by gate order but also by explicit timing primitives that define time and duration of operations. PyQASM now supports the following timing operations: Operations:- duration - A typed literal or variable representing an explicit length of time, expressed in units such as
ns
,us
,ms
, or hardware clock cycles (dt
). - stretch - A symbolic non-negative duration that is resolved at compile time into a concrete value chosen to satisfy timing constraints.
- delay - An identity operation applied to one or more qubits for a given duration, used to pin timing and prevent unintended commutation.
- box - A timing context that groups statements together with an enforced total duration, ensuring well-defined scheduling boundaries.
- angle - A fixed-width representation of phase values, useful for encoding rotations and phase shifts in hardware-specific bitstring formats.
- extern - User or backend defined functions that can be declared in QASM and validated in PyQASM for tasks such as cycle alignment or calibration.
- complex - Support for complex literals and standard functions (
abs
,real
,imag
,sin
,cos
, etc.) to describe calibrated amplitudes, phases, or control laws.