Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns::QStateDM Class Referencefinal

Density-matrix concrete QState backend using qpp::cmat. More...

#include <q2ns-qstate-dm.h>

Inheritance diagram for q2ns::QStateDM:
Collaboration diagram for q2ns::QStateDM:

Public Member Functions

 QStateDM (std::size_t numQubits)
 Construct the |0...0><0...0| state on numQubits qubits.
 
 QStateDM (qpp::cmat rho)
 Construct from an existing density matrix.
 
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.
 
std::size_t NumQubits () const override
 Return the number of logical qubits in the state.
 
void Apply (const QGate &g, const std::vector< q2ns::Index > &targets) override
 Apply a gate to the given target qubits.
 
std::shared_ptr< QStateMergeDisjoint (const QState &other) const override
 Return the disjoint merge of this state and another density-matrix backend.
 
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::cmat & GetRho () const
 Return the underlying density matrix.
 
void SetRho (const qpp::cmat &rho)
 Replace the underlying density matrix after validation.
 
std::shared_ptr< QStatePartialTrace (const std::vector< q2ns::Index > &subsystemA)
 Extract a subsystem by partial trace.
 
- Public Member Functions inherited from q2ns::QState
virtual ~QState ()
 Virtual destructor.
 
StateId GetStateId () const
 Get the registry-assigned state id.
 
void SetStateId (StateId id)
 Set the registry-assigned state id.
 
virtual void Apply (const QGate &g, const std::vector< Index > &targets)=0
 Apply a gate to the given target indices.
 

Static Private Member Functions

static void ValidateDensityMatrix (const qpp::cmat &rho)
 Validate basic density-matrix structure.
 

Private Attributes

qpp::cmat rho_
 Backend density matrix.
 

Additional Inherited Members

- Protected Member Functions inherited from q2ns::QState
void PrintHeader (std::ostream &os, const char *backendName) const
 Print a standard backend header.
 
- Static Protected Member Functions inherited from q2ns::QState
static uint64_t SplitMix64 (uint64_t x)
 Mix a 64-bit value deterministically.
 
static uint64_t DeriveSeed64 (uint32_t seed, uint32_t run, int64_t stream, uint64_t salt)
 Derive a deterministic 64-bit seed from ns-3 seed/run and a stream.
 
static std::seed_seq MakeSeedSeq (uint64_t s64)
 Build a std::seed_seq from a 64-bit seed.
 
static void CheckSeedRunNonZero (uint32_t seed, uint32_t run)
 Abort if ns-3 seed or run is zero.
 
template<uint64_t SALT>
static bool ShouldReseed (uint32_t seed, uint32_t run, int64_t stream)
 Return whether a backend-global RNG should be reseeded.
 
template<uint64_t SALT, typename ReseedFn >
static int64_t AssignStreamsGlobal (int64_t stream, ReseedFn reseed_fn)
 Helper for backends that reseed a global RNG source.
 

Detailed Description

Density-matrix concrete QState backend using qpp::cmat.

QStateDM stores an N-qubit state as a 2^N by 2^N density matrix and implements the common QState interface.

Supported behavior includes:

  • general unitary evolution
  • single-qubit measurement via basis rotation and projective measurement
  • disjoint merge by tensor product
  • partial trace extraction of subsystems

Constructors validate basic density-matrix structure such as square shape, power-of-two dimension, and trace close to one.

See also
QState
QStateKet

Definition at line 45 of file q2ns-qstate-dm.h.

Constructor & Destructor Documentation

◆ QStateDM() [1/2]

q2ns::QStateDM::QStateDM ( std::size_t  numQubits)
explicit

Construct the |0...0><0...0| state on numQubits qubits.

Parameters
numQubitsNumber of qubits.

Definition at line 80 of file q2ns-qstate-dm.cc.

References rho_, and ValidateDensityMatrix().

Here is the call graph for this function:

◆ QStateDM() [2/2]

q2ns::QStateDM::QStateDM ( qpp::cmat  rho)
explicit

Construct from an existing density matrix.

Parameters
rhoDensity matrix.
Exceptions
std::invalid_argumentif the matrix is not square, not Hermitian, not positive semidefinite, not of dimension 2^N, or does not have trace one.

Definition at line 89 of file q2ns-qstate-dm.cc.

References rho_, and ValidateDensityMatrix().

Here is the call graph for this function:

Member Function Documentation

◆ Apply()

void q2ns::QStateDM::Apply ( const QGate g,
const std::vector< q2ns::Index > &  targets 
)
override

Apply a gate to the given target qubits.

Parameters
gGate descriptor.
targetsTarget qubit indices.

Definition at line 120 of file q2ns-qstate-dm.cc.

References q2ns::QState::AssignStreamsGlobal(), q2ns::Custom, q2ns::MatrixOf(), and rho_.

Here is the call graph for this function:

◆ AssignStreams()

int64_t q2ns::QStateDM::AssignStreams ( int64_t  stream)
overridevirtual

Assign RNG streams for deterministic randomness.

This seeds qpp's random source for measurement and other stochastic qpp operations used by this backend.

Parameters
streamStarting stream index.
Returns
Number of streams consumed.

Reimplemented from q2ns::QState.

Definition at line 95 of file q2ns-qstate-dm.cc.

References q2ns::QState::AssignStreamsGlobal(), and q2ns::QState::MakeSeedSeq().

Here is the call graph for this function:

◆ GetRho()

const qpp::cmat & q2ns::QStateDM::GetRho ( ) const
inline

Return the underlying density matrix.

Returns
Reference to the backend density matrix.
See also
SetRho

Definition at line 120 of file q2ns-qstate-dm.h.

References rho_.

◆ Measure()

QState::MeasureResult q2ns::QStateDM::Measure ( q2ns::Index  target,
q2ns::Basis  basis = q2ns::Basis::Z 
)
overridevirtual

Measure one qubit in the requested basis and split the result.

The returned measured state is a 1-qubit density matrix expressed in the requested measurement basis. The survivors state contains the remaining qubits in their original relative order.

Parameters
targetIndex of the qubit to measure.
basisMeasurement basis. Defaults to Z.
Returns
Measurement result containing outcome, measured state, and survivor state.

Implements q2ns::QState.

Definition at line 156 of file q2ns-qstate-dm.cc.

References q2ns::QState::AssignStreamsGlobal(), q2ns::H, NumQubits(), rho_, q2ns::S, q2ns::X, q2ns::Y, and q2ns::Z.

Here is the call graph for this function:

◆ MergeDisjoint()

std::shared_ptr< QState > q2ns::QStateDM::MergeDisjoint ( const QState other) const
overridevirtual

Return the disjoint merge of this state and another density-matrix backend.

The merged qubit order is [this-qubits..., other-qubits...].

Parameters
otherOther state to merge with.
Returns
Newly allocated merged state.

Implements q2ns::QState.

Definition at line 144 of file q2ns-qstate-dm.cc.

References q2ns::QState::AssignStreamsGlobal(), and rho_.

Here is the call graph for this function:

◆ NumQubits()

std::size_t q2ns::QStateDM::NumQubits ( ) const
overridevirtual

Return the number of logical qubits in the state.

Returns
Number of logical qubits.

Implements q2ns::QState.

Definition at line 106 of file q2ns-qstate-dm.cc.

References q2ns::QState::AssignStreamsGlobal(), and rho_.

Referenced by Measure(), and PartialTrace().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PartialTrace()

std::shared_ptr< QState > q2ns::QStateDM::PartialTrace ( const std::vector< q2ns::Index > &  subsystemA)

Extract a subsystem by partial trace.

The qubits listed in subsystemA are kept in the returned state. This object is mutated to become the complementary subsystem.

Parameters
subsystemAIndices of the subsystem to keep in the returned state.
Returns
New state representing subsystemA.

Definition at line 204 of file q2ns-qstate-dm.cc.

References q2ns::QState::AssignStreamsGlobal(), NumQubits(), rho_, and ValidateDensityMatrix().

Here is the call graph for this function:

◆ Print()

void q2ns::QStateDM::Print ( std::ostream &  os) const
overridevirtual

Print a human-readable representation of the state.

Parameters
osOutput stream.

Implements q2ns::QState.

Definition at line 113 of file q2ns-qstate-dm.cc.

References q2ns::QState::AssignStreamsGlobal(), q2ns::QState::PrintHeader(), and rho_.

Here is the call graph for this function:

◆ SetRho()

void q2ns::QStateDM::SetRho ( const qpp::cmat &  rho)

Replace the underlying density matrix after validation.

Parameters
rhoNew density matrix.
Exceptions
std::invalid_argumentif the matrix is not square, not Hermitian, not positive semidefinite, not of dimension 2^N, or does not have trace one.
See also
GetRho

Definition at line 252 of file q2ns-qstate-dm.cc.

References q2ns::QState::AssignStreamsGlobal(), rho_, and ValidateDensityMatrix().

Here is the call graph for this function:

◆ ValidateDensityMatrix()

void q2ns::QStateDM::ValidateDensityMatrix ( const qpp::cmat &  rho)
staticprivate

Validate basic density-matrix structure.

Parameters
rhoDensity matrix to validate.

Definition at line 25 of file q2ns-qstate-dm.cc.

References q2ns::H.

Referenced by PartialTrace(), QStateDM(), QStateDM(), and SetRho().

Here is the caller graph for this function:

Member Data Documentation

◆ rho_

qpp::cmat q2ns::QStateDM::rho_
private

Backend density matrix.

Definition at line 152 of file q2ns-qstate-dm.h.

Referenced by Apply(), GetRho(), Measure(), MergeDisjoint(), NumQubits(), PartialTrace(), Print(), QStateDM(), QStateDM(), and SetRho().


The documentation for this class was generated from the following files: