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

Ket-based concrete QState backend using qpp. More...

#include <q2ns-qstate-ket.h>

Inheritance diagram for q2ns::QStateKet:
Collaboration diagram for q2ns::QStateKet:

Public Member Functions

 QStateKet (std::size_t numQubits)
 Construct the |0...0> state on numQubits qubits.
 
 QStateKet (qpp::ket state)
 Construct from an existing ket.
 
int64_t AssignStreams (int64_t stream) override
 Assign RNG streams for deterministic randomness.
 
std::size_t NumQubits () const override
 Return the number of logical qubits in the state.
 
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.
 
std::shared_ptr< QStateMergeDisjoint (const QState &other) const override
 Return the disjoint merge of this state and another ket 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::ket & GetKet () const
 Return the underlying ket.
 
void SetKet (const qpp::ket &k)
 Replace the underlying ket.
 
qpp::cmat GetDensityMatrix () const
 Return the density matrix |psi><psi| of the current ket.
 
- 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 ValidateKet (const qpp::ket &k)
 Validate basic ket properties.
 

Private Attributes

qpp::ket state_
 Pure state ket.
 

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

Ket-based concrete QState backend using qpp.

QStateKet stores an N-qubit pure state as a qpp::ket and implements the common QState interface.

This is the default backend and supports:

  • general unitary gate application
  • disjoint merge by tensor product
  • single-qubit measurement with split return semantics

It also exposes backend-specific helpers for direct ket access and conversion to a density matrix.

Constructors validate basic ket structure such as power-of-two dimension and norm close to one.

See also
QState
QStateDM

Definition at line 46 of file q2ns-qstate-ket.h.

Constructor & Destructor Documentation

◆ QStateKet() [1/2]

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

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

Parameters
numQubitsNumber of qubits.

Definition at line 51 of file q2ns-qstate-ket.cc.

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

Here is the call graph for this function:

◆ QStateKet() [2/2]

q2ns::QStateKet::QStateKet ( qpp::ket  state)
explicit

Construct from an existing ket.

Parameters
statePure state ket.
Exceptions
std::invalid_argumentif the ket is empty, not a column vector, not of dimension 2^N, or not normalized.

Definition at line 58 of file q2ns-qstate-ket.cc.

References state_, and ValidateKet().

Here is the call graph for this function:

Member Function Documentation

◆ Apply()

void q2ns::QStateKet::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 88 of file q2ns-qstate-ket.cc.

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

Here is the call graph for this function:

◆ AssignStreams()

int64_t q2ns::QStateKet::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 64 of file q2ns-qstate-ket.cc.

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

Here is the call graph for this function:

◆ GetDensityMatrix()

qpp::cmat q2ns::QStateKet::GetDensityMatrix ( ) const

Return the density matrix |psi><psi| of the current ket.

Returns
Density matrix corresponding to the current pure state.
See also
GetKet

Definition at line 179 of file q2ns-qstate-ket.cc.

References state_.

◆ GetKet()

const qpp::ket & q2ns::QStateKet::GetKet ( ) const

Return the underlying ket.

Returns
Reference to the backend ket.
See also
SetKet
GetDensityMatrix

Definition at line 166 of file q2ns-qstate-ket.cc.

References state_.

◆ Measure()

QState::MeasureResult q2ns::QStateKet::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 collapsed state 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 124 of file q2ns-qstate-ket.cc.

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

Here is the call graph for this function:

◆ MergeDisjoint()

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

Return the disjoint merge of this state and another ket 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 112 of file q2ns-qstate-ket.cc.

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

Here is the call graph for this function:

◆ NumQubits()

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

Return the number of logical qubits in the state.

Returns
Number of logical qubits.

Implements q2ns::QState.

Definition at line 75 of file q2ns-qstate-ket.cc.

References state_.

Referenced by Measure().

Here is the caller graph for this function:

◆ Print()

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

Print a human-readable representation of the state.

Parameters
osOutput stream.

Implements q2ns::QState.

Definition at line 81 of file q2ns-qstate-ket.cc.

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

Here is the call graph for this function:

◆ SetKet()

void q2ns::QStateKet::SetKet ( const qpp::ket &  k)

Replace the underlying ket.

Parameters
kNew backend ket.
Exceptions
std::invalid_argumentif the ket is empty, not a column vector, not of dimension 2^N, or not normalized.
See also
GetKet

Definition at line 172 of file q2ns-qstate-ket.cc.

References q2ns::QState::AssignStreamsGlobal(), state_, and ValidateKet().

Here is the call graph for this function:

◆ ValidateKet()

void q2ns::QStateKet::ValidateKet ( const qpp::ket &  k)
staticprivate

Validate basic ket properties.

Parameters
kKet to validate.

Definition at line 26 of file q2ns-qstate-ket.cc.

References q2ns::QState::AssignStreamsGlobal().

Referenced by QStateKet(), and SetKet().

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

Member Data Documentation

◆ state_

qpp::ket q2ns::QStateKet::state_
private

Pure state ket.

Definition at line 149 of file q2ns-qstate-ket.h.

Referenced by Apply(), GetDensityMatrix(), GetKet(), Measure(), MergeDisjoint(), NumQubits(), Print(), QStateKet(), QStateKet(), and SetKet().


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