Qx.Export.OpenQASM (Qx - Quantum Computing Simulator v0.5.0)
View SourceExport Qx quantum circuits to OpenQASM format.
This module provides functionality to convert Qx quantum circuits into OpenQASM code that can be executed on real quantum hardware platforms including:
- IBM Quantum (IBM Q)
- AWS Braket
- Google Cirq (via OpenQASM import)
- Rigetti (via AWS Braket)
- IonQ (via AWS Braket or Azure Quantum)
Supports both OpenQASM 2.0 and 3.0 specifications.
OpenQASM Versions
- OpenQASM 2.0: Legacy version, widely supported, no conditional operations
- OpenQASM 3.0: Modern version with conditionals, mid-circuit measurements, and control flow
Supported Features
- Single-qubit gates (H, X, Y, Z, S, T, RX, RY, RZ, Phase)
- Multi-qubit gates (CNOT/CX, CZ, Toffoli/CCX)
- Measurements and classical bits
- Conditional operations (OpenQASM 3.0 only)
- Barriers for visualization organization
Examples
# Export a Bell state circuit to OpenQASM 3.0
circuit = Qx.circuit(2)
|> Qx.h(0)
|> Qx.cnot(0, 1)
|> Qx.measure(0, 0)
|> Qx.measure(1, 1)
qasm = Qx.Export.OpenQASM.to_qasm(circuit)
File.write!("bell_state.qasm", qasm)
# Export to OpenQASM 2.0 (no conditionals)
qasm2 = Qx.Export.OpenQASM.to_qasm(circuit, version: 2)
# Export with custom options
qasm = Qx.Export.OpenQASM.to_qasm(circuit,
version: 3,
include_comments: true,
gate_style: :verbose
)Platform Compatibility
| Platform | Version | Mid-circuit Measurement | Conditionals |
|---|---|---|---|
| IBM Quantum | 2.0, 3.0 | 3.0 only | 3.0 only |
| AWS Braket | 3.0 | Yes | Yes |
| Google Cirq | 2.0 | No | No |
| Rigetti | 2.0, 3.0 | 3.0 only | 3.0 only |
Limitations
- Circuits with conditionals cannot be exported to OpenQASM 2.0
- Custom gate definitions are expanded to standard gates
- Qubit ordering follows MSB convention (qubit 0 is leftmost)
Summary
Functions
Converts a Qx quantum circuit to OpenQASM format.
Functions
Converts a Qx quantum circuit to OpenQASM format.
Parameters
circuit- AQx.QuantumCircuitstructoptions- Keyword list of options (default: [])
Options
:version- OpenQASM version (2 or 3, default: 3):include_comments- Add descriptive comments (default: false):gate_style- Gate naming style (:standardor:verbose, default::standard)
Returns
A string containing the OpenQASM program.
Raises
Qx.GateError- If circuit contains unsupported gatesQx.ConditionalError- If circuit has conditionals but version is 2ArgumentError- If invalid options are provided
Examples
circuit = Qx.circuit(2) |> Qx.h(0) |> Qx.cnot(0, 1)
qasm = Qx.Export.OpenQASM.to_qasm(circuit)
# Output:
# OPENQASM 3.0;
# include "stdgates.inc";
#
# qubit[2] q;
# bit[2] c;
#
# h q[0];
# cx q[0], q[1];