Usage Examples
Compilation
Inlining & Unrolling
This example demonstrates the capabilities of the PyQASM tool by unrolling a complex OpenQASM program into a simplified form.
In PyQASM, “unrolling” refers to the process of simplifying a quantum program by expanding custom gate definitions and flattening complex language constructs like subroutines, loops, and conditional statements into basic operations. This technique, also known as program flattening or inlining, transforms nested and recursive structures into a linear sequence of operations consisting solely of qubit and classical bit declarations, gate operations, and measurement operations. By converting the program into this simplified format, it becomes easier to perform subsequent transpilation or compilation steps before executing the program on a quantum device.
For complete details about the supported operations, see PyQASM Supported Operations.
Analysis
num_qubits
, num_clbits
, and depth
This example demonstrates how to use the num_qubits
, num_clbits
, and depth
methods.
Transformations
has_measurements
and remove_measurements
This example demonstrates how to check for measurements and remove them.
Very similar to the previous example, we also have has_barriers
and remove_barriers
methods
to check for barriers and remove them, respectively.
populate_idle_qubits
This example demonstrates how to populate idle qubits with identity gates -
remove_idle_qubits
This example demonstrates how to remove idle qubits from the module and shows the qubit count before and after.
reverse_qubit_order
This example demonstrates how to reverse the order of qubits in the module with a more involved program.
rebase
Unroll and convert an QASM program to a specified basis gate set.
The target basis set can be chosen from the options present in the pyqasm.elements.BasisSet
class. Currently we
support conversion to the following basis sets:
BasisSet.CLIFFORD_T
:{"h", "t", "s", "cx", "tdg", "sdg"}
BasisSet.ROTATIONAL_CX
:{"rx", "ry", "rz", "cx"}
For example -
The rebase
functionality is useful for converting a program to a basis set that is supported by a specific
quantum device. This conversion can be extended to new custom basis sets by adding the required gate decompositions
to the pyqasm.maps.decomposition_rules
module.
Currently we have complete conversion support for the BasisSet.ROTATIONAL_CX
basis set,
whereas only non-parameterized gate conversion is available for BasisSet.CLIFFORD_T
.
Refer to the PyQASM API Documentation for more details on the available methods and their usage.