Table of Contents
- Introduction
- What Is OpenQASM?
- Motivation for Assembly-Level Control
- Versions of OpenQASM
- OpenQASM Syntax Basics
- Declaring Qubits and Classical Bits
- Gate Definitions and Standard Gates
- Custom Gates and Subroutines
- Measurement Syntax
- Conditional Statements and Classical Control
- Mid-Circuit Measurement
- Reset Operations and Initialization
- Looping and Branching (OpenQASM 3.0)
- Classical Expressions and Registers
- Interfacing with Qiskit
- Exporting Circuits as OpenQASM
- Importing OpenQASM into Qiskit
- Use Cases for OpenQASM
- Limitations and Low-Level Optimization
- Conclusion
1. Introduction
OpenQASM (Quantum Assembly Language) is an intermediate representation used to express quantum programs in a human-readable assembly-like syntax. It enables low-level access and control over quantum circuits.
2. What Is OpenQASM?
Developed by IBM, OpenQASM is the standard language for describing quantum circuits at the gate level, bridging the gap between high-level quantum programming and quantum hardware.
3. Motivation for Assembly-Level Control
- Direct control over gate execution
- Real-time measurement and feedback
- Hardware-specific optimizations
- Interoperability across platforms
4. Versions of OpenQASM
- OpenQASM 2.0: Used in IBM Quantum systems and Qiskit
- OpenQASM 3.0: Experimental, adds classical flow control and timing features
5. OpenQASM Syntax Basics
OpenQASM programs start with:
OPENQASM 2.0;
include "qelib1.inc";
6. Declaring Qubits and Classical Bits
qreg q[2];
creg c[2];
7. Gate Definitions and Standard Gates
h q[0];
cx q[0], q[1];
x q[1];
8. Custom Gates and Subroutines
gate mygate a,b {
h a;
cx a, b;
}
mygate q[0], q[1];
9. Measurement Syntax
measure q[0] -> c[0];
10. Conditional Statements and Classical Control
if (c==1) x q[1];
Performs a gate if a classical register equals a given value.
11. Mid-Circuit Measurement
measure q[0] -> c[0];
if (c==1) x q[1];
Supported on some devices and simulators.
12. Reset Operations and Initialization
reset q[0];
Used to reinitialize a qubit to |0⟩.
13. Looping and Branching (OpenQASM 3.0)
for i in [0:10] {
x q[i];
}
while (c[0] == 0) {
z q[1];
}
14. Classical Expressions and Registers
int[32] counter = 0;
bit x;
bool flag = true;
These constructs exist in OpenQASM 3.0 and enhance classical programmability.
15. Interfacing with Qiskit
Export a circuit to OpenQASM:
qasm_str = qc.qasm()
Load from OpenQASM:
from qiskit import QuantumCircuit
qc = QuantumCircuit.from_qasm_str(qasm_str)
16. Exporting Circuits as OpenQASM
with open('my_circuit.qasm', 'w') as f:
f.write(qasm_str)
17. Importing OpenQASM into Qiskit
qc = QuantumCircuit.from_qasm_file('my_circuit.qasm')
18. Use Cases for OpenQASM
- Hardware compilation interfaces
- Program interoperability
- Custom transpilation workflows
- Debugging and verification
19. Limitations and Low-Level Optimization
- Manual gate management is error-prone
- Platform-dependent features not fully portable
- Limited abstraction compared to SDKs
20. Conclusion
OpenQASM provides precise, low-level control over quantum programs and acts as a bridge between quantum software frameworks and real quantum hardware. With Qiskit support and OpenQASM 3.0’s new features, it continues to play a vital role in quantum software development.