q-rust

Crates.ioq-rust
lib.rsq-rust
version0.1.1
created_at2025-11-04 06:23:56.367392+00
updated_at2025-11-23 04:12:04.917172+00
descriptionA modular quantum transpiler and QASM 2.0 parser written in Rust.
homepage
repositoryhttps://github.com/Arturacu/Q-Rust
max_upload_size
id1915837
size41,217
(Arturacu)

documentation

README

Q-Rust

A quantum transpiler written in Rust, designed to parse, analyze, and optimize quantum circuits.

Crates.io Build Status

Features

  • Robust QASM 2.0 Support: Parse OpenQASM 2.0 files with full support for standard gates, parameterized rotations, and measurements.
  • Modular Intermediate Representation (IR): A flexible, type-safe IR built for easy analysis and transformation.
  • Graph-Based Backend Topology: Define custom hardware backends with arbitrary connectivity using efficient graph structures (petgraph).

Error Handling

Q-Rust enforces strict OpenQASM 2.0 compliance. It will explicitly reject:

  • OpenQASM 3.0: Files starting with OPENQASM 3.0; will trigger an Unsupported OpenQASM version error.
  • Missing Headers: Files must start with a valid OPENQASM 2.0; header.
  • Missing Measurements: The Circuit::validate() method warns if a circuit has no measurements, as it will not produce output on real hardware.

Installation

Add to your Cargo.toml:

[dependencies]
q-rust = "0.1"

Usage

Parsing QASM

use q_rust::parser::parse_qasm;

fn main() {
    let qasm_code = r#"
        OPENQASM 2.0;
        include "qelib1.inc";
        qreg q[2];
        creg c[2];
        h q[0];
        cx q[0], q[1];
        measure q[1] -> c[1];
    "#;

    match parse_qasm(qasm_code) {
        Ok(circuit) => {
            println!("Successfully parsed circuit with {} operations.", circuit.operations.len());
            println!("Qubits: {}, Classical Bits: {}", circuit.num_qubits, circuit.num_cbits);
        }
        Err(e) => eprintln!("Error parsing QASM: {}", e),
    }
}

Defining a Backend

use q_rust::backend::Backend;

fn main() {
    let mut backend = Backend::new("MyQuantumMachine".to_string(), 5);
    
    // Define connectivity (0 -> 1 -> 2)
    backend.set_coupling_map(vec![(0, 1), (1, 2)]);
    
    println!("Backend {} has {} qubits.", backend.name, backend.num_qubits);
}

References

This project adheres to the OpenQASM 2.0 specification:

License

MIT or Apache-2.0

Commit count: 0

cargo fmt