Qx.Export.OpenQASM (Qx - Quantum Computing Simulator v0.5.0)

View Source

Export 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

PlatformVersionMid-circuit MeasurementConditionals
IBM Quantum2.0, 3.03.0 only3.0 only
AWS Braket3.0YesYes
Google Cirq2.0NoNo
Rigetti2.0, 3.03.0 only3.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

to_qasm(circuit, options \\ [])

Converts a Qx quantum circuit to OpenQASM format.

Parameters

Options

  • :version - OpenQASM version (2 or 3, default: 3)
  • :include_comments - Add descriptive comments (default: false)
  • :gate_style - Gate naming style (:standard or :verbose, default: :standard)

Returns

A string containing the OpenQASM program.

Raises

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];