Skip to main content

zeta_quantum/
lib.rs

1//! Copyright (c) 2026 Zeta Reticula Inc
2//! Licensed under the MIT License. See LICENSE for details.
3
4// zeta-quantum/src/lib.rs (updated integration)
5pub mod api;
6pub mod bms;
7pub mod cache;
8pub mod cost;
9pub mod error;
10pub mod flux_holonomy;
11pub mod ir;
12pub mod nonlocal_dynamics;
13pub mod phi_ir;
14pub mod qpu;
15pub mod radiative_entropy;
16
17pub use bms::{BMSObservable, EscapeRoute};
18pub use error::{ZetaError, ZetaResult};
19pub use flux_holonomy::{compute_flux_sector, FluxSector};
20pub use nonlocal_dynamics::{
21    evolve_nonlocal_lindblad, reduce_to_subsystem, von_neumann_entropy, EntropicSubsystem,
22};
23pub use phi_ir::{PhiCircuit, PhiElement, WeylGen};
24pub use qpu::{Modality, QPU};
25pub use radiative_entropy::RadiativeVisibility;
26
27use nalgebra::DMatrix;
28use std::collections::HashMap;
29
30/// The main quantization engine that converts quantum circuits to the phi language
31///
32/// This engine takes a quantum circuit in the phi language and converts it to a format
33/// that can be executed on a quantum processing unit (QPU).
34///
35/// The quantization process involves:
36/// 1. Converting the phi circuit to a format suitable for the QPU
37/// 2. Optimizing the circuit for the specific QPU architecture
38/// 3. Calculating the integrated obstruction for the circuit
39/// 4. Returning the optimized circuit and the integrated obstruction
40#[derive(Debug)]
41pub struct QuantumQuantizer {
42    pub qpu: QPU, // The quantum processing unit to use for quantization
43    pub calibration: HashMap<String, f64>, // Calibration data for the QPU
44}
45
46// The implementation of the QuantumQuantizer struct
47impl QuantumQuantizer {
48    // Constructor for the QuantumQuantizer struct
49    pub fn new(
50        modality: Modality,                 // The modality of the QPU
51        calibration: &HashMap<String, f64>, // Calibration data for the QPU
52        calibration_ts: String,             // Timestamp of the calibration data
53    ) -> Self {
54        let mut qpu = QPU::new(modality, calibration_ts); // Create a new QPU with the given modality and calibration timestamp
55
56        // Minimal coupling map bootstrap: parse keys like "0-1" => err
57        for (k, &err) in calibration {
58            // Split the key by '-' and parse the two parts as u32
59            if let Some((a, b)) = k.split_once('-') {
60                // Parse the two parts as u32
61                if let (Ok(a), Ok(b)) = (a.parse::<u32>(), b.parse::<u32>()) {
62                    qpu.add_coupling(a, b, err); // Add the coupling to the QPU
63                    qpu.add_coupling(b, a, err); // Add the coupling to the QPU
64                }
65            }
66        }
67
68        // Return the QuantumQuantizer
69        Self {
70            qpu,                              // The quantum processing unit to use for quantization
71            calibration: calibration.clone(), // Calibration data for the QPU
72        }
73    }
74
75    // Quantize a circuit
76    pub fn quantize(&mut self, circ: &PhiCircuit) -> anyhow::Result<(PhiCircuit, f64)> {
77        // For now the optimizer is identity; return integrated obstruction S_X.
78        let integrated_obstruction = crate::cost::integrated_obstruction(circ, &self.qpu); // Calculate the integrated obstruction
79        Ok((circ.clone(), integrated_obstruction)) // Return the optimized circuit and the integrated obstruction
80    }
81
82    // Quantize a circuit with BMS observable
83    pub fn quantize_with_bms(
84        &mut self,
85        circ: &PhiCircuit,
86        route: EscapeRoute,
87    ) -> anyhow::Result<(PhiCircuit, f64, BMSObservable)> {
88        let (optimized, sx) = self.quantize(circ)?; // Quantize the circuit
89        let (_entropy, bms_obs) =
90            crate::bms::probe_gravitational_memory(&optimized, &self.qpu, route); // Probe the gravitational memory
91        Ok((optimized, sx, bms_obs)) // Return the optimized circuit, the integrated obstruction, and the BMS observable
92    }
93
94    fn analyze_nonlocal_dynamics(&self, dt: f64, dissipator: f64) -> EntropicSubsystem {
95        let rho_full = DMatrix::identity(4, 4);
96        let h_nonlocal = DMatrix::from_fn(4, 4, |i, j| if i % 2 != j % 2 { 0.1 } else { 0.0 });
97        let rho_evolved = evolve_nonlocal_lindblad(&rho_full, &h_nonlocal, dissipator, dt);
98        reduce_to_subsystem(&rho_evolved, 1)
99    }
100
101    fn analyze_flux_sector(&self) -> FluxSector {
102        let proj1 = DMatrix::from_diagonal_element(4, 4, 1.0);
103        let proj2 = proj1.clone();
104        let flux_op = DMatrix::from_element(4, 4, 0.05);
105        let holonomy_mat = DMatrix::identity(4, 4) + flux_op.clone() * 0.2;
106        compute_flux_sector(&proj1, &proj2, &flux_op, &holonomy_mat)
107    }
108
109    fn analyze_radiative_visibility(
110        &self,
111        bms: &BMSObservable,
112        integrated_obstruction: f64,
113    ) -> RadiativeVisibility {
114        RadiativeVisibility::from_bms_and_entropy(bms, integrated_obstruction)
115    }
116
117    /// v0.6.0: quantize + full subsystems/sectors
118    pub fn quantize_full_analysis(
119        &mut self,
120        circ: &PhiCircuit,
121        bms_route: crate::bms::EscapeRoute,
122        dt: f64,
123        dissipator: f64,
124    ) -> anyhow::Result<(
125        PhiCircuit,
126        f64,
127        crate::bms::BMSObservable,
128        EntropicSubsystem,
129        FluxSector,
130        RadiativeVisibility,
131    )> {
132        let (optimized, integrated_obstruction) = self.quantize(circ)?;
133        let (_full_entropy, bms_obs) =
134            crate::bms::probe_gravitational_memory(&optimized, &self.qpu, bms_route);
135
136        let subsystem = self.analyze_nonlocal_dynamics(dt, dissipator);
137        let flux_sector = self.analyze_flux_sector();
138        let radiative = self.analyze_radiative_visibility(&bms_obs, integrated_obstruction);
139
140        Ok((
141            optimized,
142            integrated_obstruction,
143            bms_obs,
144            subsystem,
145            flux_sector,
146            radiative,
147        ))
148    }
149}