2 releases
new 0.1.0-alpha.2 | May 15, 2025 |
---|---|
0.1.0-alpha.1 | May 13, 2025 |
#329 in Simulation
92 downloads per month
Used in 5 crates
43KB
961 lines
QuantRS2-Core: Foundational Quantum Types
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 identifierRegister<N>
: Fixed-size quantum register using const genericsQuantRS2Error
: Error enumeration for all possible errorsQuantRS2Result<T>
: Convenient result type alias
Important Traits
GateOp
: Trait for quantum gates with matrix representationControlledOp
: Trait for controlled quantum operationsStateVector
: 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:
- quantrs2-circuit: Provides core types used in circuit construction
- quantrs2-sim: Provides matrix representations used in simulation
License
This project is licensed under either:
at your option.
Dependencies
~0.4–1MB
~20K SLoC