Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-qstate-stab.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------
2 * Q2NS - Quantum Network Simulator
3 * Copyright (c) 2026 quantuminternet.it
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *---------------------------------------------------------------------------*/
9/**
10 * @file q2ns-qstate-stab.h
11 * @brief Declares q2ns::QStateStab, a stabilizer QState backend using
12 * softwareQinc/stab.
13 *
14 * This backend targets correct implementation of the q2ns::QState interface
15 * for Clifford-state simulation.
16 */
17
18#pragma once
19
20#include "AffineState.h"
21
22#include "ns3/q2ns-qstate.h"
23
24#include <memory>
25#include <vector>
26
27namespace q2ns {
28
29/**
30 * @ingroup q2ns_qstate
31 * @class QStateStab
32 * @brief Stabilizer concrete QState backend using stab::AffineState.
33 *
34 * QStateStab supports Clifford-state simulation using a vendored
35 * stab::AffineState implementation.
36 *
37 * Supported operations include:
38 * - 1-qubit Clifford gates I, H, S, SDG, X, Y, Z
39 * - 2-qubit Clifford gates CNOT, CZ, SWAP
40 * - single-qubit projective measurement in X, Y, and Z bases
41 * - disjoint merge by tensor product
42 *
43 * This backend does not support arbitrary custom unitary gates.
44 *
45 * @see QState
46 * @see QGateKind
47 */
48class QStateStab final : public QState {
49public:
50 /**
51 * @brief Construct the |0...0> stabilizer state on numQubits qubits.
52 * @param numQubits Number of qubits.
53 */
54 explicit QStateStab(std::size_t numQubits);
55
56 /**
57 * @brief Assign RNG streams for deterministic randomness.
58 *
59 * This seeds the underlying stab random source used by measurement and other
60 * stochastic backend operations.
61 *
62 * @param stream Starting stream index.
63 * @return Number of streams consumed.
64 */
66
67 /**
68 * @brief Print a human-readable representation of the state.
69 * @param os Output stream.
70 */
71 void Print(std::ostream& os) const override;
72
73 /**
74 * @brief Apply a supported Clifford gate to the given target qubits.
75 * @param g Gate descriptor.
76 * @param t Target qubit indices.
77 */
78 void Apply(const QGate& g, const std::vector<Index>& t) override;
79
80 /**
81 * @brief Return the number of logical qubits in the state.
82 * @return Number of logical qubits.
83 */
84 std::size_t NumQubits() const override;
85
86 /**
87 * @brief Measure one qubit in the requested basis and split the result.
88 *
89 * The returned measured state is a 1-qubit stabilizer eigenstate in the
90 * requested basis. The survivors state contains the remaining qubits in
91 * their original relative order.
92 *
93 * @param target Index of the qubit to measure.
94 * @param basis Measurement basis. Defaults to Z.
95 * @return Measurement result containing outcome, measured state, and survivor state.
96 */
98
99 /**
100 * @brief Return the disjoint merge of this state and another stabilizer backend.
101 *
102 * The merged qubit order is [this-qubits..., other-qubits...].
103 *
104 * @param other Other state to merge with.
105 * @return Newly allocated merged state.
106 */
107 std::shared_ptr<QState> MergeDisjoint(const QState& other) const override;
108
109 /**
110 * @brief Return a clone of this stabilizer state.
111 * @return Newly allocated copy of this state.
112 */
113 std::shared_ptr<QState> Clone() const {
114 return std::make_shared<QStateStab>(*this);
115 }
116
117 /**
118 * @brief Return the underlying AffineState.
119 * @return Reference to the backend stabilizer state.
120 */
121 const stab::AffineState& GetAffineState() const {
122 return psi_;
123 }
124
125private:
126 /**
127 * @brief Rotate a local basis so that measuring Z implements the requested basis.
128 *
129 * X measurement is implemented as H then Z.
130 * Y measurement is implemented as SDG then H then Z.
131 *
132 * @param q Target qubit.
133 * @param basis Requested measurement basis.
134 */
136
137 /**
138 * @brief Build a 1-qubit basis eigenstate for a measurement outcome.
139 * @param basis Measurement basis.
140 * @param bit Outcome bit.
141 * @return Newly allocated 1-qubit stabilizer state.
142 */
143 static std::shared_ptr<QStateStab> Synth1QEigenstate_(Basis basis, int bit);
144
145 /**
146 * @brief Remove one qubit while preserving survivor order.
147 *
148 * The target is swapped to the end and then dropped from the underlying
149 * AffineState.
150 *
151 * @param target Qubit to remove.
152 */
154
155 std::size_t numQubits_{0}; //!< Number of logical qubits.
156 stab::AffineState psi_; //!< Underlying stabilizer state.
157};
158
159} // namespace q2ns
Lightweight gate descriptor used by QState backends.
Definition q2ns-qgate.h:68
Stabilizer concrete QState backend using stab::AffineState.
MeasureResult Measure(Index target, Basis basis=Basis::Z) override
Measure one qubit in the requested basis and split the result.
const stab::AffineState & GetAffineState() const
Return the underlying AffineState.
void RemoveQubit_(Index target)
Remove one qubit while preserving survivor order.
std::size_t numQubits_
Number of logical qubits.
void RotateIntoZBasis_(Index q, Basis basis)
Rotate a local basis so that measuring Z implements the requested basis.
std::shared_ptr< QState > Clone() const
Return a clone of this stabilizer state.
std::shared_ptr< QState > MergeDisjoint(const QState &other) const override
Return the disjoint merge of this state and another stabilizer backend.
std::size_t NumQubits() const override
Return the number of logical qubits in the state.
static std::shared_ptr< QStateStab > Synth1QEigenstate_(Basis basis, int bit)
Build a 1-qubit basis eigenstate for a measurement outcome.
stab::AffineState psi_
Underlying stabilizer state.
void Print(std::ostream &os) const override
Print a human-readable representation of the state.
void Apply(const QGate &g, const std::vector< Index > &t) override
Apply a supported Clifford gate to the given target qubits.
int64_t AssignStreams(int64_t stream) override
Assign RNG streams for deterministic randomness.
Backend-agnostic interface for a quantum state object.
Definition q2ns-qstate.h:51
static int64_t AssignStreamsGlobal(int64_t stream, ReseedFn reseed_fn)
Helper for backends that reseed a global RNG source.
Basis
Measurement basis for single-qubit projective measurement.
Definition q2ns-types.h:88
std::size_t Index
Generic qubit index type within a backend state.
Definition q2ns-types.h:49
Result of measuring one qubit and splitting the state.