Download src/lib.rs from Snapkitty/sovereign-compiler: direct link, hf CLI and curl.
- Browser
- Download file 5.29 kB
-
https://huggingface.co/Snapkitty/sovereign-compiler/resolve/main/src/lib.rs
- Command line
-
hf download hf://Snapkitty/sovereign-compiler/src/lib.rs
-
curl -L -o lib.rs https://huggingface.co/Snapkitty/sovereign-compiler/resolve/main/src/lib.rs
5.29 kB
| //! # utqc-core | |
| //! | |
| //! Circuit IR — Gate, Qubit, Circuit, Measurement. | |
| //! Non-recursive. Every circuit compiles to a flat list of operations. | |
| use serde::{Deserialize, Serialize}; | |
| use thiserror::Error; | |
| /// Errors in circuit construction or execution. | |
| pub enum CircuitError { | |
| /// Qubit index out of bounds. | |
| QubitOutOfBounds(usize, usize), | |
| /// Duplicate measurement on the same qubit. | |
| DuplicateMeasurement(usize), | |
| /// Empty circuit. | |
| EmptyCircuit, | |
| } | |
| /// A single qubit identifier. | |
| pub struct Qubit(pub usize); | |
| /// Single-qubit gate types. | |
| pub enum SingleGate { | |
| /// Pauli-X (NOT). | |
| PauliX, | |
| /// Pauli-Y. | |
| PauliY, | |
| /// Pauli-Z. | |
| PauliZ, | |
| /// Hadamard. | |
| Hadamard, | |
| /// T-gate (π/8 phase). | |
| TGate, | |
| /// S-gate (π/4 phase). | |
| SGate, | |
| } | |
| /// Two-qubit gate types. | |
| pub enum DoubleGate { | |
| /// Controlled-NOT. | |
| CNOT, | |
| /// Controlled-Z. | |
| CZ, | |
| /// SWAP. | |
| SWAP, | |
| } | |
| /// A gate operation in the circuit. | |
| pub enum Gate { | |
| /// Single-qubit gate. | |
| Single { | |
| /// Gate type. | |
| gate: SingleGate, | |
| /// Target qubit. | |
| target: Qubit, | |
| }, | |
| /// Two-qubit gate. | |
| Double { | |
| /// Gate type. | |
| gate: DoubleGate, | |
| /// Control qubit. | |
| control: Qubit, | |
| /// Target qubit. | |
| target: Qubit, | |
| }, | |
| /// Rotation gate (parameterized). | |
| Rotation { | |
| /// Target qubit. | |
| target: Qubit, | |
| /// Angle in radians. | |
| angle: f64, | |
| }, | |
| } | |
| /// A measurement record. | |
| pub struct Measurement { | |
| /// Qubit being measured. | |
| pub qubit: Qubit, | |
| /// Classical bit index to store result. | |
| pub classical_bit: usize, | |
| } | |
| /// A quantum circuit — non-recursive flat IR. | |
| pub struct Circuit { | |
| /// Number of qubits in the circuit. | |
| pub num_qubits: usize, | |
| /// Number of classical bits. | |
| pub num_classical_bits: usize, | |
| /// Ordered list of gate operations. | |
| pub gates: Vec<Gate>, | |
| /// Measurements to perform at the end. | |
| pub measurements: Vec<Measurement>, | |
| } | |
| impl Circuit { | |
| /// Create a new empty circuit. | |
| pub fn new(num_qubits: usize, num_classical_bits: usize) -> Self { | |
| Self { | |
| num_qubits, | |
| num_classical_bits, | |
| gates: Vec::new(), | |
| measurements: Vec::new(), | |
| } | |
| } | |
| /// Add a gate to the circuit. | |
| pub fn add_gate(&mut self, gate: Gate) -> Result<(), CircuitError> { | |
| match &gate { | |
| Gate::Single { target, .. } => { | |
| if target.0 >= self.num_qubits { | |
| return Err(CircuitError::QubitOutOfBounds(target.0, self.num_qubits)); | |
| } | |
| } | |
| Gate::Double { control, target, .. } => { | |
| if control.0 >= self.num_qubits { | |
| return Err(CircuitError::QubitOutOfBounds(control.0, self.num_qubits)); | |
| } | |
| if target.0 >= self.num_qubits { | |
| return Err(CircuitError::QubitOutOfBounds(target.0, self.num_qubits)); | |
| } | |
| } | |
| Gate::Rotation { target, .. } => { | |
| if target.0 >= self.num_qubits { | |
| return Err(CircuitError::QubitOutOfBounds(target.0, self.num_qubits)); | |
| } | |
| } | |
| } | |
| self.gates.push(gate); | |
| Ok(()) | |
| } | |
| /// Add a measurement. | |
| pub fn add_measurement(&mut self, qubit: Qubit, classical_bit: usize) -> Result<(), CircuitError> { | |
| if qubit.0 >= self.num_qubits { | |
| return Err(CircuitError::QubitOutOfBounds(qubit.0, self.num_qubits)); | |
| } | |
| if self.measurements.iter().any(|m| m.qubit == qubit) { | |
| return Err(CircuitError::DuplicateMeasurement(qubit.0)); | |
| } | |
| self.measurements.push(Measurement { qubit, classical_bit }); | |
| Ok(()) | |
| } | |
| /// Number of gates in the circuit. | |
| pub fn depth(&self) -> usize { | |
| self.gates.len() | |
| } | |
| /// Validate the circuit. | |
| pub fn validate(&self) -> Result<(), CircuitError> { | |
| if self.gates.is_empty() && self.measurements.is_empty() { | |
| return Err(CircuitError::EmptyCircuit); | |
| } | |
| Ok(()) | |
| } | |
| } | |
| /// The non-recursive pass trait. | |
| pub trait Pass { | |
| /// Input type for this pass. | |
| type Input; | |
| /// Output type for this pass. | |
| type Output; | |
| /// Name of this pass. | |
| fn name(&self) -> &'static str; | |
| /// Execute the pass. | |
| fn run(&self, input: Self::Input) -> Result<Self::Output, CircuitError>; | |
| } | |