Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-qstate-ket.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-ket.h
11 * @brief Declares q2ns::QStateKet, a ket-based QState backend using qpp.
12 */
13
14#pragma once
15
16#include <qpp/qpp.hpp>
17
18#include "ns3/q2ns-qstate.h"
19
20#include <memory>
21#include <vector>
22
23namespace q2ns {
24
25/**
26 * @ingroup q2ns_qstate
27 * @class QStateKet
28 * @brief Ket-based concrete QState backend using qpp.
29 *
30 * QStateKet stores an N-qubit pure state as a qpp::ket and implements the
31 * common QState interface.
32 *
33 * This is the default backend and supports:
34 * - general unitary gate application
35 * - disjoint merge by tensor product
36 * - single-qubit measurement with split return semantics
37 *
38 * It also exposes backend-specific helpers for direct ket access and
39 * conversion to a density matrix.
40 *
41 * Constructors validate basic ket structure such as power-of-two dimension and norm close to one.
42 *
43 * @see QState
44 * @see QStateDM
45 */
46class QStateKet final : public QState {
47public:
48 /**
49 * @brief Construct the |0...0> state on numQubits qubits.
50 * @param numQubits Number of qubits.
51 */
52 explicit QStateKet(std::size_t numQubits);
53
54 /**
55 * @brief Construct from an existing ket.
56 * @param state Pure state ket.
57 * @throws std::invalid_argument if the ket is empty, not a column vector,
58 * not of dimension 2^N, or not normalized.
59 */
60 explicit QStateKet(qpp::ket state);
61
62 /**
63 * @brief Assign RNG streams for deterministic randomness.
64 *
65 * This seeds qpp's random source for measurement and other stochastic qpp
66 * operations used by this backend.
67 *
68 * @param stream Starting stream index.
69 * @return Number of streams consumed.
70 */
72
73 /**
74 * @brief Return the number of logical qubits in the state.
75 * @return Number of logical qubits.
76 */
77 std::size_t NumQubits() const override;
78
79 /**
80 * @brief Print a human-readable representation of the state.
81 * @param os Output stream.
82 */
83 void Print(std::ostream& os) const override;
84
85 /**
86 * @brief Apply a gate to the given target qubits.
87 * @param g Gate descriptor.
88 * @param targets Target qubit indices.
89 */
90 void Apply(const QGate& g, const std::vector<q2ns::Index>& targets) override;
91
92 /**
93 * @brief Return the disjoint merge of this state and another ket backend.
94 *
95 * The merged qubit order is [this-qubits..., other-qubits...].
96 *
97 * @param other Other state to merge with.
98 * @return Newly allocated merged state.
99 */
100 std::shared_ptr<QState> MergeDisjoint(const QState& other) const override;
101
102 /**
103 * @brief Measure one qubit in the requested basis and split the result.
104 *
105 * The returned measured state is a 1-qubit collapsed state expressed in the
106 * requested measurement basis. The survivors state contains the remaining
107 * qubits in their original relative order.
108 *
109 * @param target Index of the qubit to measure.
110 * @param basis Measurement basis. Defaults to Z.
111 * @return Measurement result containing outcome, measured state, and survivor state.
112 */
114
115 /**
116 * @brief Return the underlying ket.
117 * @return Reference to the backend ket.
118 *
119 * @see SetKet
120 * @see GetDensityMatrix
121 */
122 const qpp::ket& GetKet() const;
123
124 /**
125 * @brief Replace the underlying ket.
126 * @param k New backend ket.
127 * @throws std::invalid_argument if the ket is empty, not a column vector,
128 * not of dimension 2^N, or not normalized.
129 *
130 * @see GetKet
131 */
132 void SetKet(const qpp::ket& k);
133
134 /**
135 * @brief Return the density matrix |psi><psi| of the current ket.
136 * @return Density matrix corresponding to the current pure state.
137 *
138 * @see GetKet
139 */
140 qpp::cmat GetDensityMatrix() const;
141
142private:
143 /**
144 * @brief Validate basic ket properties.
145 * @param k Ket to validate.
146 */
147 static void ValidateKet(const qpp::ket& k);
148
149 qpp::ket state_; //!< Pure state ket.
150};
151
152} // namespace q2ns
Lightweight gate descriptor used by QState backends.
Definition q2ns-qgate.h:68
Ket-based concrete QState backend using qpp.
int64_t AssignStreams(int64_t stream) override
Assign RNG streams for deterministic randomness.
void Print(std::ostream &os) const override
Print a human-readable representation of the state.
qpp::cmat GetDensityMatrix() const
Return the density matrix |psi><psi| of the current ket.
qpp::ket state_
Pure state ket.
static void ValidateKet(const qpp::ket &k)
Validate basic ket properties.
MeasureResult Measure(q2ns::Index target, q2ns::Basis basis=q2ns::Basis::Z) override
Measure one qubit in the requested basis and split the result.
const qpp::ket & GetKet() const
Return the underlying ket.
std::size_t NumQubits() const override
Return the number of logical qubits in the state.
std::shared_ptr< QState > MergeDisjoint(const QState &other) const override
Return the disjoint merge of this state and another ket backend.
void SetKet(const qpp::ket &k)
Replace the underlying ket.
void Apply(const QGate &g, const std::vector< q2ns::Index > &targets) override
Apply a gate to the given target qubits.
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.