#quantum-simulation #quantum-computing #gate #annealing #computing #quantum

quantrs2-core

Core types and traits for the QuantRS2 quantum computing framework

2 releases

new 0.1.0-alpha.2 May 15, 2025
0.1.0-alpha.1 May 13, 2025

#329 in Simulation

Download history 92/week @ 2025-05-07

92 downloads per month
Used in 5 crates

MIT/Apache

43KB
961 lines

QuantRS2-Core: Foundational Quantum Types

Crates.io Documentation License

QuantRS2-Core is part of the QuantRS2 quantum computing framework, providing the foundational types and traits that form the basis of the entire ecosystem.

Features

  • Type-Safe Qubit Identifiers: Zero-cost abstractions for qubit references
  • Comprehensive Gate Definitions: Standard quantum gates with matrix representations
  • Register Abstractions: Quantum register types with const generics for compile-time validation
  • Robust Error Handling: Comprehensive error types and result wrappers

Usage

Basic Example

use quantrs2_core::prelude::*;

fn main() -> QuantRS2Result<()> {
    // Create a qubit identifier
    let qubit = QubitId::new(0);
    
    // Use a standard gate
    let x_gate = XGate::new();
    let matrix = x_gate.matrix();
    
    // Create a register (with 2 qubits)
    let register = Register::<2>::new();
    
    Ok(())
}

Creating Custom Gates

use quantrs2_core::prelude::*;
use num_complex::Complex64;

struct MyCustomGate;

impl GateOp for MyCustomGate {
    fn matrix(&self) -> Vec<Complex64> {
        // Define your custom gate matrix here
        // Example: custom rotation gate
        vec![
            Complex64::new(0.5, 0.0), Complex64::new(0.5, 0.5),
            Complex64::new(0.5, -0.5), Complex64::new(0.5, 0.0),
        ]
    }
    
    fn n_qubits(&self) -> usize {
        1 // This is a single-qubit gate
    }
}

Module Structure

  • error.rs: Error types and result wrappers
  • gate.rs: Gate trait definitions and standard gate implementations
  • qubit.rs: Qubit identifier type
  • register.rs: Quantum register abstractions

API Overview

Core Types

  • QubitId: Type-safe wrapper around a qubit identifier
  • Register<N>: Fixed-size quantum register using const generics
  • QuantRS2Error: Error enumeration for all possible errors
  • QuantRS2Result<T>: Convenient result type alias

Important Traits

  • GateOp: Trait for quantum gates with matrix representation
  • ControlledOp: Trait for controlled quantum operations
  • StateVector: Trait for quantum state representations

Implementation Notes

  • The QubitId type is a zero-cost abstraction using #[repr(transparent)]
  • Gates are represented by their matrix form in column-major order
  • Registers use Rust's const generics feature for compile-time qubit count validation
  • Error handling follows Rust's standard practices with a custom error type

Future Plans

See TODO.md for planned improvements and features.

Integration with Other QuantRS2 Modules

This module is designed to work seamlessly with:

License

This project is licensed under either:

at your option.

Dependencies

~0.4–1MB
~20K SLoC