14#include "ns3/q2ns-qstate-ket.h"
16#include "ns3/assert.h"
31 throw std::invalid_argument(
"QStateKet: ket must be non-empty");
35 throw std::invalid_argument(
"QStateKet: ket must be a column vector");
38 const double log2dim = std::log2(
static_cast<double>(
k.rows()));
40 throw std::invalid_argument(
"QStateKet: dim(ket) must be 2^N for some integer N");
43 const double norm2 = std::real((
k.adjoint() *
k)(0, 0));
45 throw std::invalid_argument(
"QStateKet: ket must be normalized");
69 qpp::RandomDevices::get_instance().get_prng().seed(
seq);
76 return static_cast<std::size_t
>(std::llround(std::log2(
state_.rows())));
89 std::vector<qpp::idx>
t;
92 t.push_back(
static_cast<qpp::idx
>(
i));
102 const std::size_t
k =
targets.size();
103 const std::size_t
dim = 1ull <<
k;
105 "QStateKet::Apply: wrong gate dimension");
115 throw std::runtime_error(
"QStateKet::MergeDisjoint: incompatible backend");
119 return std::make_shared<QStateKet>(
combined);
126 throw std::out_of_range(
"QStateKet::Measure: target index out of range");
146 qpp::ket
oneQubit = (
res == 0 ? qpp::mket({0}) : qpp::mket({1}));
160 auto survivors = std::make_shared<QStateKet>(std::move(
states[
res]));
Lightweight gate descriptor used by QState backends.
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.
QStateKet(std::size_t numQubits)
Construct the |0...0> state on numQubits qubits.
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.
static std::seed_seq MakeSeedSeq(uint64_t s64)
Build a std::seed_seq from a 64-bit seed.
void PrintHeader(std::ostream &os, const char *backendName) const
Print a standard backend header.
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.
std::size_t Index
Generic qubit index type within a backend state.
@ Custom
Caller-supplied matrix.
const Matrix & MatrixOf(QGateKind k)
Return the built-in matrix for a non-custom gate kind.
Result of measuring one qubit and splitting the state.