cqlib_core/circuit/gate/classical_data.rs
1// This code is part of Cqlib.
2//
3// (C) Copyright China Telecom Quantum Group 2026
4//
5// This code is licensed under the Apache License, Version 2.0. You may
6// obtain a copy of this license in the LICENSE.txt file in the root directory
7// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8//
9// Any modifications or derivative works of this code must retain this
10// copyright notice, and modified files need to carry a notice indicating
11// that they have been altered from the originals.
12
13//! Side-effecting runtime classical data instructions.
14//!
15//! These instructions connect quantum measurements and typed classical
16//! storage. Pure computations remain represented by
17//! [`ClassicalExpr`].
18
19use crate::circuit::{ClassicalExpr, ClassicalValue, ClassicalVar};
20use std::fmt;
21
22/// Runtime classical data operation.
23///
24/// These operations are side-effecting classical updates in the circuit
25/// schedule. They produce or overwrite runtime classical storage, while
26/// [`ClassicalExpr`] remains side-effect-free and only reads runtime classical
27/// variables or immutable values.
28#[derive(Debug, Clone, PartialEq)]
29pub enum ClassicalDataOp {
30 /// Stores an expression value into a mutable classical variable.
31 Store {
32 /// Mutable storage location being updated.
33 target: ClassicalVar,
34 /// Type-compatible value expression to store.
35 value: ClassicalExpr,
36 },
37 /// Measures one qubit and produces an immutable `Bit` value.
38 MeasureBit {
39 /// Immutable value receiving the measurement result.
40 result: ClassicalValue,
41 },
42 /// Measures several qubits and produces an immutable `BitVec(width)` value.
43 ///
44 /// The operation qubit order is the bit-vector order, with qubit index 0
45 /// mapped to the least-significant bit.
46 MeasureBits {
47 /// Immutable bit-vector value receiving the packed result.
48 result: ClassicalValue,
49 },
50}
51
52impl ClassicalDataOp {
53 /// Returns the stable snake-case operation name.
54 pub const fn name(&self) -> &'static str {
55 match self {
56 Self::Store { .. } => "store",
57 Self::MeasureBit { .. } => "measure_bit",
58 Self::MeasureBits { .. } => "measure_bits",
59 }
60 }
61
62 /// Returns the mutable store target, if this is a store operation.
63 pub fn target(&self) -> Option<ClassicalVar> {
64 match self {
65 Self::Store { target, .. } => Some(*target),
66 Self::MeasureBit { .. } | Self::MeasureBits { .. } => None,
67 }
68 }
69
70 /// Returns the immutable measurement result, if this is a measurement.
71 pub fn result(&self) -> Option<ClassicalValue> {
72 match self {
73 Self::Store { .. } => None,
74 Self::MeasureBit { result } | Self::MeasureBits { result } => Some(*result),
75 }
76 }
77
78 /// Returns the stored expression, if this is a store operation.
79 pub fn value(&self) -> Option<&ClassicalExpr> {
80 match self {
81 Self::Store { value, .. } => Some(value),
82 Self::MeasureBit { .. } | Self::MeasureBits { .. } => None,
83 }
84 }
85}
86
87impl fmt::Display for ClassicalDataOp {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 f.write_str(self.name())
90 }
91}