Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-qstate-dm.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-dm.h
11 * @brief Declares q2ns::QStateDM, a density-matrix 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 QStateDM
28 * @brief Density-matrix concrete QState backend using qpp::cmat.
29 *
30 * QStateDM stores an N-qubit state as a 2^N by 2^N density matrix and
31 * implements the common QState interface.
32 *
33 * Supported behavior includes:
34 * - general unitary evolution
35 * - single-qubit measurement via basis rotation and projective measurement
36 * - disjoint merge by tensor product
37 * - partial trace extraction of subsystems
38 *
39 * Constructors validate basic density-matrix structure such as square shape,
40 * power-of-two dimension, and trace close to one.
41 *
42 * @see QState
43 * @see QStateKet
44 */
45class QStateDM final : public QState {
46public:
47 /**
48 * @brief Construct the |0...0><0...0| state on numQubits qubits.
49 * @param numQubits Number of qubits.
50 */
51 explicit QStateDM(std::size_t numQubits);
52
53 /**
54 * @brief Construct from an existing density matrix.
55 * @param rho Density matrix.
56 * @throws std::invalid_argument if the matrix is not square, not Hermitian,
57 * not positive semidefinite, not of dimension 2^N, or does not have trace one.
58 */
59 explicit QStateDM(qpp::cmat rho);
60
61 /**
62 * @brief Assign RNG streams for deterministic randomness.
63 *
64 * This seeds qpp's random source for measurement and other stochastic qpp
65 * operations used by this backend.
66 *
67 * @param stream Starting stream index.
68 * @return Number of streams consumed.
69 */
71
72 /**
73 * @brief Print a human-readable representation of the state.
74 * @param os Output stream.
75 */
76 void Print(std::ostream& os) const override;
77
78 /**
79 * @brief Return the number of logical qubits in the state.
80 * @return Number of logical qubits.
81 */
82 std::size_t NumQubits() const override;
83
84 /**
85 * @brief Apply a gate to the given target qubits.
86 * @param g Gate descriptor.
87 * @param targets Target qubit indices.
88 */
89 void Apply(const QGate& g, const std::vector<q2ns::Index>& targets) override;
90
91 /**
92 * @brief Return the disjoint merge of this state and another density-matrix backend.
93 *
94 * The merged qubit order is [this-qubits..., other-qubits...].
95 *
96 * @param other Other state to merge with.
97 * @return Newly allocated merged state.
98 */
99 std::shared_ptr<QState> MergeDisjoint(const QState& other) const override;
100
101 /**
102 * @brief Measure one qubit in the requested basis and split the result.
103 *
104 * The returned measured state is a 1-qubit density matrix expressed in the
105 * requested measurement basis. The survivors state contains the remaining
106 * qubits in their original relative order.
107 *
108 * @param target Index of the qubit to measure.
109 * @param basis Measurement basis. Defaults to Z.
110 * @return Measurement result containing outcome, measured state, and survivor state.
111 */
113
114 /**
115 * @brief Return the underlying density matrix.
116 * @return Reference to the backend density matrix.
117 *
118 * @see SetRho
119 */
120 const qpp::cmat& GetRho() const {
121 return rho_;
122 }
123
124 /**
125 * @brief Replace the underlying density matrix after validation.
126 * @param rho New density matrix.
127 * @throws std::invalid_argument if the matrix is not square, not Hermitian,
128 * not positive semidefinite, not of dimension 2^N, or does not have trace one.
129 *
130 * @see GetRho
131 */
132 void SetRho(const qpp::cmat& rho);
133
134 /**
135 * @brief Extract a subsystem by partial trace.
136 *
137 * The qubits listed in subsystemA are kept in the returned state. This object
138 * is mutated to become the complementary subsystem.
139 *
140 * @param subsystemA Indices of the subsystem to keep in the returned state.
141 * @return New state representing subsystemA.
142 */
143 std::shared_ptr<QState> PartialTrace(const std::vector<q2ns::Index>& subsystemA);
144
145private:
146 /**
147 * @brief Validate basic density-matrix structure.
148 * @param rho Density matrix to validate.
149 */
150 static void ValidateDensityMatrix(const qpp::cmat& rho);
151
152 qpp::cmat rho_; //!< Backend density matrix.
153};
154
155} // namespace q2ns
Lightweight gate descriptor used by QState backends.
Definition q2ns-qgate.h:68
Density-matrix concrete QState backend using qpp::cmat.
MeasureResult Measure(q2ns::Index target, q2ns::Basis basis=q2ns::Basis::Z) override
Measure one qubit in the requested basis and split the result.
static void ValidateDensityMatrix(const qpp::cmat &rho)
Validate basic density-matrix structure.
std::size_t NumQubits() const override
Return the number of logical qubits in the state.
std::shared_ptr< QState > PartialTrace(const std::vector< q2ns::Index > &subsystemA)
Extract a subsystem by partial trace.
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.
void Apply(const QGate &g, const std::vector< q2ns::Index > &targets) override
Apply a gate to the given target qubits.
const qpp::cmat & GetRho() const
Return the underlying density matrix.
qpp::cmat rho_
Backend density matrix.
std::shared_ptr< QState > MergeDisjoint(const QState &other) const override
Return the disjoint merge of this state and another density-matrix backend.
void SetRho(const qpp::cmat &rho)
Replace the underlying density matrix after validation.
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.