cqlib-core 0.1.0-beta.1

The high-performance core foundation for the CQLib quantum computing ecosystem.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// This code is part of Cqlib.
//
// (C) Copyright China Telecom Quantum Group 2026
//
// This code is licensed under the Apache License, Version 2.0. You may
// obtain a copy of this license in the LICENSE.txt file in the root directory
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

//! Runtime decomposition rules.
//!
//! This module provides pass-local templates for algorithmic decomposition
//! results. These rules are generated from synthesized value operations and
//! replayed by role, not by concrete qubit id. They deliberately stay separate
//! from the static knowledge-rule library because decomposition templates may
//! depend on resource signatures such as clean or dirty ancillary usage.

use crate::circuit::{
    Instruction, Parameter, ParameterValue, Qubit, StandardGate, ValueInstruction, ValueOperation,
};
use crate::compile::CompilerError;
use crate::compile::transform::decompose::unitary::TwoQubitSynthesisTarget;
use crate::compile::transform::target_basis::TargetBasisSignature;
use ndarray::Array2;
use num_complex::Complex64;
use smallvec::SmallVec;
use std::collections::HashMap;

/// Pass-local cache for runtime decomposition rules.
#[derive(Debug, Clone, Default)]
pub struct DecompositionRuleCache {
    mc_gate_rules: HashMap<McGateRuleKey, DecompositionRule>,
    numeric_unitary_rules: HashMap<NumericUnitaryRuleKey, NumericUnitaryRule>,
    stats: DecompositionRuleStats,
}

/// Basic cache accounting used by tests and diagnostics.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct DecompositionRuleStats {
    pub hits: usize,
    pub misses: usize,
    pub inserts: usize,
}

impl DecompositionRuleCache {
    /// Instantiates a cached multi-controlled-gate decomposition rule.
    ///
    /// Returns `Ok(None)` when the cache has no matching rule. The supplied
    /// qubit slices provide the concrete role bindings for a cached template.
    pub fn instantiate_mc_gate(
        &mut self,
        request: McGateRuleRequest<'_>,
        controls: &[Qubit],
        targets: &[Qubit],
        ancillas: &[Qubit],
    ) -> Result<Option<Vec<ValueOperation>>, CompilerError> {
        let key = McGateRuleKey::from_request(request);
        if let Some(rule) = self.mc_gate_rules.get(&key) {
            self.stats.hits += 1;
            return Ok(Some(rule.instantiate(controls, targets, ancillas)?));
        }
        self.stats.misses += 1;
        Ok(None)
    }

    /// Records a synthesized multi-controlled-gate decomposition as a runtime rule.
    ///
    /// `operations` must use only the supplied controls, targets, and ancillas.
    /// The generated rule is independent of the concrete qubit ids and can be
    /// instantiated later with any qubits that have the same role counts.
    pub fn insert_mc_gate(
        &mut self,
        request: McGateRuleRequest<'_>,
        controls: &[Qubit],
        targets: &[Qubit],
        ancillas: &[Qubit],
        operations: &[ValueOperation],
    ) -> Result<(), CompilerError> {
        let key = McGateRuleKey::from_request(request);
        let rule = DecompositionRule::from_operations(controls, targets, ancillas, operations)?;
        self.mc_gate_rules.insert(key, rule);
        self.stats.inserts += 1;
        Ok(())
    }

    /// Returns cache hit/miss/insert counters.
    pub const fn stats(&self) -> DecompositionRuleStats {
        self.stats
    }

    /// Instantiates a cached numeric unitary decomposition rule.
    ///
    /// Returns `Ok(None)` when no bit-exact matrix and basis match exists.
    pub fn instantiate_numeric_unitary(
        &mut self,
        request: NumericUnitaryRuleRequest<'_>,
        qubits: &[Qubit],
    ) -> Result<Option<(Vec<ValueOperation>, f64)>, CompilerError> {
        let key = NumericUnitaryRuleKey::from_request(request);
        if let Some(rule) = self.numeric_unitary_rules.get(&key) {
            self.stats.hits += 1;
            return Ok(Some((
                rule.rule.instantiate(&[], qubits, &[])?,
                rule.phase_delta,
            )));
        }
        self.stats.misses += 1;
        Ok(None)
    }

    /// Records a synthesized numeric unitary decomposition as a runtime rule.
    ///
    /// The numeric matrix key is bit-exact: mathematically equivalent matrices
    /// with different floating-point representations are intentionally distinct.
    pub fn insert_numeric_unitary(
        &mut self,
        request: NumericUnitaryRuleRequest<'_>,
        qubits: &[Qubit],
        operations: &[ValueOperation],
        phase_delta: f64,
    ) -> Result<(), CompilerError> {
        let key = NumericUnitaryRuleKey::from_request(request);
        let rule = DecompositionRule::from_operations(&[], qubits, &[], operations)?;
        self.numeric_unitary_rules
            .insert(key, NumericUnitaryRule { rule, phase_delta });
        self.stats.inserts += 1;
        Ok(())
    }
}

/// Runtime cache request for one multi-controlled-gate synthesis.
#[derive(Debug, Clone, Copy)]
pub struct McGateRuleRequest<'a> {
    pub gate: StandardGate,
    pub control_count: usize,
    pub target_count: usize,
    pub params: &'a [ParameterValue],
    pub resource: ResourceSignature,
}

/// Runtime cache request for one numeric unitary synthesis.
#[derive(Debug, Clone)]
pub struct NumericUnitaryRuleRequest<'a> {
    pub num_qubits: u16,
    pub matrix: &'a Array2<Complex64>,
    pub synthesis_key: NumericUnitarySynthesisKey,
}

/// Semantic context that determines numeric-unitary synthesis output.
///
/// This key intentionally includes the canonical target-basis identity and
/// exactness mode, so a cached template is never reused under a different
/// lowering policy.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum NumericUnitarySynthesisKey {
    OneQubit,
    TwoQubit {
        target_basis: Option<TargetBasisSignature>,
        fallback_pauli: bool,
    },
}

impl NumericUnitarySynthesisKey {
    pub const fn one_qubit() -> Self {
        Self::OneQubit
    }

    pub(crate) fn two_qubit(target: &TwoQubitSynthesisTarget) -> Self {
        Self::TwoQubit {
            target_basis: target.cache_signature(),
            fallback_pauli: target.fallback_pauli(),
        }
    }
}

#[derive(Debug, Clone)]
struct NumericUnitaryRule {
    rule: DecompositionRule,
    phase_delta: f64,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct NumericUnitaryRuleKey {
    num_qubits: u16,
    rows: usize,
    cols: usize,
    matrix: Vec<(u64, u64)>,
    synthesis_key: NumericUnitarySynthesisKey,
}

impl NumericUnitaryRuleKey {
    fn from_request(request: NumericUnitaryRuleRequest<'_>) -> Self {
        Self {
            num_qubits: request.num_qubits,
            rows: request.matrix.nrows(),
            cols: request.matrix.ncols(),
            matrix: request
                .matrix
                .iter()
                .map(|value| (value.re.to_bits(), value.im.to_bits()))
                .collect(),
            synthesis_key: request.synthesis_key,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct McGateRuleKey {
    gate: StandardGate,
    control_count: usize,
    target_count: usize,
    params: Vec<ParameterValueKey>,
    resource: ResourceSignature,
}

impl McGateRuleKey {
    fn from_request(request: McGateRuleRequest<'_>) -> Self {
        Self {
            gate: request.gate,
            control_count: request.control_count,
            target_count: request.target_count,
            params: request.params.iter().map(ParameterValueKey::from).collect(),
            resource: request.resource,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum ParameterValueKey {
    Fixed(u64),
    Param(Parameter),
}

impl From<&ParameterValue> for ParameterValueKey {
    fn from(value: &ParameterValue) -> Self {
        match value {
            ParameterValue::Fixed(value) => Self::Fixed(value.to_bits()),
            ParameterValue::Param(parameter) => Self::Param(parameter.clone()),
        }
    }
}

/// Resource-dependent synthesis signature for a runtime rule.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ResourceSignature {
    pub algorithm: DecompositionAlgorithm,
    pub ancilla_kind: RuntimeAncillaKind,
    pub ancilla_count: usize,
}

impl ResourceSignature {
    /// Creates a signature for an ancillary-free decomposition.
    pub const fn no_aux(algorithm: DecompositionAlgorithm) -> Self {
        Self {
            algorithm,
            ancilla_kind: RuntimeAncillaKind::None,
            ancilla_count: 0,
        }
    }

    /// Creates a signature for a clean-ancilla decomposition.
    pub const fn clean(algorithm: DecompositionAlgorithm, count: usize) -> Self {
        Self {
            algorithm,
            ancilla_kind: RuntimeAncillaKind::CleanZero,
            ancilla_count: count,
        }
    }

    /// Creates a signature for a dirty-ancilla decomposition.
    pub const fn dirty(algorithm: DecompositionAlgorithm, count: usize) -> Self {
        Self {
            algorithm,
            ancilla_kind: RuntimeAncillaKind::Dirty,
            ancilla_count: count,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RuntimeAncillaKind {
    None,
    CleanZero,
    Dirty,
}

/// Algorithm identity used to prevent incompatible template reuse.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DecompositionAlgorithm {
    PauliSmall,
    PauliTwoClean,
    PauliOneCleanKg24,
    PauliManyClean,
    PauliManyDirty,
    PauliTwoDirty,
    PauliOneDirty,
    PauliOneCleanB95,
    PauliNoAux,
    CleanAccumulator,
    NoAux,
}

#[derive(Debug, Clone)]
pub struct DecompositionRule {
    operations: Vec<TemplateOperation>,
}

impl DecompositionRule {
    /// Builds a runtime rule from concrete value operations.
    ///
    /// The resulting rule abstracts all qubits into control, target, and
    /// ancilla roles. The operation sequence may later be instantiated with
    /// different concrete qubits that preserve the same role counts.
    pub fn from_operations(
        controls: &[Qubit],
        targets: &[Qubit],
        ancillas: &[Qubit],
        operations: &[ValueOperation],
    ) -> Result<Self, CompilerError> {
        let mut template_operations = Vec::with_capacity(operations.len());
        for operation in operations {
            let ValueInstruction::Instruction(instruction) = &operation.instruction else {
                return Err(CompilerError::InvariantViolation(
                    "runtime decomposition rules support only gate-like value instructions"
                        .to_string(),
                ));
            };
            let qubits = operation
                .qubits
                .iter()
                .map(|qubit| template_qubit(*qubit, controls, targets, ancillas))
                .collect::<Result<SmallVec<[_; 3]>, _>>()?;
            template_operations.push(TemplateOperation {
                instruction: instruction.clone(),
                qubits,
                params: operation.params.clone(),
                label: operation.label.clone(),
            });
        }
        Ok(Self {
            operations: template_operations,
        })
    }

    /// Instantiates this rule with concrete control, target, and ancilla qubits.
    pub fn instantiate(
        &self,
        controls: &[Qubit],
        targets: &[Qubit],
        ancillas: &[Qubit],
    ) -> Result<Vec<ValueOperation>, CompilerError> {
        self.operations
            .iter()
            .map(|operation| operation.instantiate(controls, targets, ancillas))
            .collect()
    }
}

#[derive(Debug, Clone)]
struct TemplateOperation {
    instruction: Instruction,
    qubits: SmallVec<[TemplateQubit; 3]>,
    params: SmallVec<[ParameterValue; 1]>,
    label: Option<Box<str>>,
}

impl TemplateOperation {
    fn instantiate(
        &self,
        controls: &[Qubit],
        targets: &[Qubit],
        ancillas: &[Qubit],
    ) -> Result<ValueOperation, CompilerError> {
        let qubits = self
            .qubits
            .iter()
            .map(|qubit| qubit.instantiate(controls, targets, ancillas))
            .collect::<Result<SmallVec<[_; 3]>, _>>()?;
        Ok(ValueOperation {
            instruction: ValueInstruction::from_instruction(self.instruction.clone()),
            qubits,
            params: self.params.clone(),
            label: self.label.clone(),
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TemplateQubit {
    Control(usize),
    Target(usize),
    Ancilla(usize),
}

impl TemplateQubit {
    fn instantiate(
        self,
        controls: &[Qubit],
        targets: &[Qubit],
        ancillas: &[Qubit],
    ) -> Result<Qubit, CompilerError> {
        match self {
            Self::Control(index) => controls.get(index).copied(),
            Self::Target(index) => targets.get(index).copied(),
            Self::Ancilla(index) => ancillas.get(index).copied(),
        }
        .ok_or_else(|| {
            CompilerError::InvariantViolation(format!(
                "runtime decomposition rule references missing template qubit {self:?}"
            ))
        })
    }
}

fn template_qubit(
    qubit: Qubit,
    controls: &[Qubit],
    targets: &[Qubit],
    ancillas: &[Qubit],
) -> Result<TemplateQubit, CompilerError> {
    if let Some(index) = controls.iter().position(|control| *control == qubit) {
        return Ok(TemplateQubit::Control(index));
    }
    if let Some(index) = targets.iter().position(|target| *target == qubit) {
        return Ok(TemplateQubit::Target(index));
    }
    if let Some(index) = ancillas.iter().position(|ancilla| *ancilla == qubit) {
        return Ok(TemplateQubit::Ancilla(index));
    }
    Err(CompilerError::InvariantViolation(format!(
        "runtime decomposition rule output uses qubit {qubit} outside source operands and ancillas"
    )))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::circuit::{Instruction, Parameter, Qubit, StandardGate, ValueOperation};
    use crate::compile::transform::decompose::unitary::TwoQubitSynthesisTarget;
    use ndarray::Array2;
    use num_complex::Complex64;
    use smallvec::smallvec;

    fn request<'a>(
        params: &'a [ParameterValue],
        resource: ResourceSignature,
    ) -> McGateRuleRequest<'a> {
        McGateRuleRequest {
            gate: StandardGate::RX,
            control_count: 2,
            target_count: 1,
            params,
            resource,
        }
    }

    #[test]
    fn cached_rule_instantiates_on_different_qubits() {
        let params = [ParameterValue::Param(Parameter::symbol("theta"))];
        let resource = ResourceSignature::clean(DecompositionAlgorithm::CleanAccumulator, 1);
        let mut cache = DecompositionRuleCache::default();
        let operations = vec![
            ValueOperation {
                instruction: ValueInstruction::from_instruction(Instruction::Standard(
                    StandardGate::CX,
                )),
                qubits: smallvec![Qubit::new(0), Qubit::new(3)],
                params: smallvec![],
                label: None,
            },
            ValueOperation {
                instruction: ValueInstruction::from_instruction(Instruction::Standard(
                    StandardGate::RX,
                )),
                qubits: smallvec![Qubit::new(2)],
                params: smallvec![params[0].clone()],
                label: None,
            },
        ];

        assert!(
            cache
                .instantiate_mc_gate(
                    request(&params, resource),
                    &[Qubit::new(0), Qubit::new(1)],
                    &[Qubit::new(2)],
                    &[Qubit::new(3)]
                )
                .unwrap()
                .is_none()
        );
        cache
            .insert_mc_gate(
                request(&params, resource),
                &[Qubit::new(0), Qubit::new(1)],
                &[Qubit::new(2)],
                &[Qubit::new(3)],
                &operations,
            )
            .unwrap();

        let instantiated = cache
            .instantiate_mc_gate(
                request(&params, resource),
                &[Qubit::new(5), Qubit::new(6)],
                &[Qubit::new(7)],
                &[Qubit::new(8)],
            )
            .unwrap()
            .unwrap();

        assert_eq!(
            instantiated[0].qubits.as_slice(),
            &[Qubit::new(5), Qubit::new(8)]
        );
        assert_eq!(instantiated[1].qubits.as_slice(), &[Qubit::new(7)]);
        assert_eq!(
            cache.stats(),
            DecompositionRuleStats {
                hits: 1,
                misses: 1,
                inserts: 1,
            }
        );
    }

    #[test]
    fn resource_signature_separates_otherwise_equal_rules() {
        let params = [ParameterValue::Fixed(0.25)];
        let mut cache = DecompositionRuleCache::default();
        let operations = vec![ValueOperation::from_standard(
            StandardGate::RX,
            [Qubit::new(2)],
            [params[0].clone()],
        )];
        cache
            .insert_mc_gate(
                request(
                    &params,
                    ResourceSignature::no_aux(DecompositionAlgorithm::NoAux),
                ),
                &[Qubit::new(0), Qubit::new(1)],
                &[Qubit::new(2)],
                &[],
                &operations,
            )
            .unwrap();

        assert!(
            cache
                .instantiate_mc_gate(
                    request(
                        &params,
                        ResourceSignature::clean(DecompositionAlgorithm::CleanAccumulator, 1),
                    ),
                    &[Qubit::new(0), Qubit::new(1)],
                    &[Qubit::new(2)],
                    &[Qubit::new(3)],
                )
                .unwrap()
                .is_none()
        );
    }

    #[test]
    fn numeric_two_qubit_cache_separates_target_signatures() {
        let matrix = Array2::<Complex64>::eye(4);
        let cx_target = TwoQubitSynthesisTarget::from_standard_gates(
            vec![StandardGate::U, StandardGate::H],
            vec![StandardGate::CX],
            true,
        )
        .unwrap();
        let cz_target = TwoQubitSynthesisTarget::from_standard_gates(
            vec![StandardGate::U, StandardGate::H],
            vec![StandardGate::CZ],
            true,
        )
        .unwrap();
        let request = NumericUnitaryRuleRequest {
            num_qubits: 2,
            matrix: &matrix,
            synthesis_key: NumericUnitarySynthesisKey::two_qubit(&cx_target),
        };
        let distinct_target_request = NumericUnitaryRuleRequest {
            num_qubits: 2,
            matrix: &matrix,
            synthesis_key: NumericUnitarySynthesisKey::two_qubit(&cz_target),
        };
        let qubits = [Qubit::new(0), Qubit::new(1)];
        let operations = vec![ValueOperation::from_standard(StandardGate::CX, qubits, [])];
        let mut cache = DecompositionRuleCache::default();

        assert!(
            cache
                .instantiate_numeric_unitary(request.clone(), &qubits)
                .unwrap()
                .is_none()
        );
        cache
            .insert_numeric_unitary(request.clone(), &qubits, &operations, 0.0)
            .unwrap();
        assert!(
            cache
                .instantiate_numeric_unitary(request, &[Qubit::new(2), Qubit::new(3)])
                .unwrap()
                .is_some()
        );
        assert!(
            cache
                .instantiate_numeric_unitary(distinct_target_request, &qubits)
                .unwrap()
                .is_none()
        );
        assert_eq!(
            cache.stats(),
            DecompositionRuleStats {
                hits: 1,
                misses: 2,
                inserts: 1,
            }
        );
    }
}