21#ifndef _SIMULATOR_STATE_H_
22#define _SIMULATOR_STATE_H_
28#include <unordered_map>
29#include <unordered_set>
34#include "framework/linalg/vector.hpp"
41template <
typename Time>
class IOperation;
130 const std::string value = std::to_string(seed);
135 uint64_t value = seed + 0x9e3779b97f4a7c15ULL * (stream + 1);
136 value = (value ^ (value >> 30)) * 0xbf58476d1ce4e5b9ULL;
137 value = (value ^ (value >> 27)) * 0x94d049bb133111ebULL;
138 return value ^ (value >> 31);
163 size_t num_qubits, std::vector<std::complex<double>> &litudes) = 0;
179 size_t num_qubits, AER::Vector<std::complex<double>> &litudes) = 0;
195 Eigen::VectorXcd &litudes) = 0;
218 throw std::runtime_error(
219 "This simulator does not support initialization to a computational "
233 const std::vector<bool> &basisState) {
236 throw std::runtime_error(
237 "This simulator does not support initialization to a computational "
262 const std::vector<std::pair<Types::qubit_t, double>> &mixture) {
265 throw std::runtime_error(
266 "This simulator does not support initialization to a mixture of "
267 "computational basis states");
283 const std::vector<std::pair<std::vector<bool>,
double>> &mixture) {
286 throw std::runtime_error(
287 "This simulator does not support initialization to a mixture of "
288 "computational basis states");
317 const std::vector<long long int> &initialMap) {}
403 virtual void Configure(
const char *key,
const char *value) = 0;
489 throw std::runtime_error(
490 "This simulator does not support exact quantum-channel evolution");
495 throw std::runtime_error(
"This simulator does not expose a density-matrix trace");
498 throw std::runtime_error(
"This simulator does not expose density-matrix purity");
501 throw std::runtime_error(
"This simulator does not expose Tr(rho^2)");
504 throw std::runtime_error(
"This simulator does not support density-matrix overlap");
507 throw std::runtime_error(
"This simulator does not expose a Hermiticity residual");
510 throw std::runtime_error(
"This simulator does not expose a Hermiticity test");
513 throw std::runtime_error(
"This simulator does not support partial trace");
516 throw std::runtime_error(
"This simulator does not support mixed-state fidelity");
519 throw std::runtime_error(
"This simulator cannot restore density-matrix trace");
522 throw std::runtime_error(
"This simulator cannot hermitize its density matrix");
525 throw std::runtime_error(
"This simulator is not a matrix-product operator");
528 throw std::runtime_error(
"This simulator is not a matrix-product operator");
547 const std::vector<double> &probabilities) {
577 double errorProbability) {
584 double mixingProbability) {
601 if (!std::isfinite(duration) || duration < 0.0)
602 throw std::invalid_argument(
603 "T1-relaxation duration must be finite and nonnegative");
604 if (std::isnan(t1) || t1 <= 0.0)
605 throw std::invalid_argument(
606 "T1 must be positive (infinity is allowed)");
608 std::isinf(t1) ? 0.0 : -std::expm1(-duration / t1);
623 if (!std::isfinite(duration) || duration < 0.0)
624 throw std::invalid_argument(
625 "Phase-damping duration must be finite and nonnegative");
626 if (std::isnan(tPhi) || tPhi <= 0.0)
627 throw std::invalid_argument(
628 "T_phi must be positive (infinity is allowed)");
630 std::isinf(tPhi) ? 0.0 : -std::expm1(-2.0 * duration / tPhi);
635 double excitedStatePopulation) {
638 gamma, excitedStatePopulation));
642 double t1,
double t2,
643 double excitedStatePopulation = 0.0) {
646 duration, t1, t2, excitedStatePopulation));
651 double probability) {
660 double correlation) {
668 double errorProbability) {
676 double mixingProbability) {
764 virtual std::unordered_map<Types::qubit_t, Types::qubit_t>
SampleCounts(
805 observers.insert(observer);
817 observers.erase(observer);
977 virtual const std::unordered_map<std::string, std::string>&
GetConfigMap()
const = 0;
992 void Notify() { notifyObservers =
true; }
1002 if (!notifyObservers)
return;
1004 for (
auto &observer : observers) {
1005 observer->Update(affectedQubits);
1011 std::unordered_set<std::shared_ptr<ISimulatorObserver>>
1013 bool notifyObservers =
Backend-independent constructors for local CPTP quantum channels.
Interface class for a quantum computing simulator state.
virtual void ApplyReset(const Types::qubits_vector &qubits)=0
Performs a reset of the specified qubits.
virtual bool IsQcsim() const =0
Returns if the simulator is a qcsim simulator.
virtual void SaveStateToInternalDestructive()=0
Saves the state to internal storage.
void ApplyTwoQubitDepolarizingNoise(Types::qubit_t qubit0, Types::qubit_t qubit1, double errorProbability)
void Notify()
Starts notifying observers.
virtual double getGrowthFactorSwap() const
virtual void SetSeed(uint64_t seed)
Seed every random stream owned by this simulator.
virtual std::vector< double > Probabilities(const Types::qubits_vector &qubits)=0
Returns the probabilities of the specified outcomes.
void ApplyTwoQubitDepolarizingMixingNoise(Types::qubit_t qubit0, Types::qubit_t qubit1, double mixingProbability)
void ApplyPauliChannel(Types::qubit_t qubit, double px, double py, double pz)
Apply a single-qubit Pauli channel specified by X/Y/Z probabilities.
virtual void SetInitialQubitsMap(const std::vector< long long int > &initialMap)
Sets the initial qubits map, if possible.
virtual bool IsDensityMatrixHermitian(double=1e-10) const
virtual void SetUpcomingGates(const std::vector< std::shared_ptr< Circuits::IOperation< double > > > &)
Supplies upcoming gates for lookahead swap optimization.
void ApplyPhaseDampingFromTime(Types::qubit_t qubit, double duration, double tPhi)
Pure phase damping for a physical duration and T_phi.
void UnregisterObserver(const std::shared_ptr< ISimulatorObserver > &observer)
Unregisters an observer.
virtual void InitializeToBasisState(size_t num_qubits, const std::vector< bool > &basisState)
Initializes the state to a computational basis state.
virtual void RestoreState()=0
Restores the state from the internally saved state.
void ApplyCorrelatedPhaseFlipNoise(Types::qubit_t qubit0, Types::qubit_t qubit1, double probability)
virtual void SaveState()=0
Saves the state to internal storage.
virtual std::complex< double > ProjectOnZero()=0
Projects the state onto the zero state.
virtual double DensityMatrixPurity() const
virtual std::complex< double > DensityMatrixOverlap(const IState &) const
void ApplyKrausChannel(const Types::qubits_vector &targets, const QuantumChannel::KrausOperators &krausOperators)
Apply an arbitrary CPTP map supplied in Kraus form.
virtual double Probability(Types::qubit_t outcome)=0
Returns the probability of the specified outcome.
virtual double DensityMatrixHermiticityResidual() const
virtual void Initialize()=0
Initializes the state.
void ApplyPhaseFlipNoise(Types::qubit_t qubit, double probability)
virtual size_t AllocateQubits(size_t num_qubits)=0
Allocates qubits.
virtual void ReCanonicalizeMatrixProductOperator()
void DontNotify()
Stops notifying observers.
void ApplyBitFlipNoise(Types::qubit_t qubit, double probability)
void ApplyDepolarizingNoise(Types::qubit_t qubit, double errorProbability)
Depolarizing noise using total nonidentity-Pauli probability p.
virtual void InitializeState(size_t num_qubits, std::vector< std::complex< double > > &litudes)=0
Initializes the state.
virtual void setGrowthFactorGate(double factor)
void ApplyT1Relaxation(Types::qubit_t qubit, double gamma)
Alias for the exact T1 amplitude-damping channel.
virtual std::vector< double > AllProbabilities()=0
Returns the probabilities of all possible outcomes.
virtual void RestoreDensityMatrixTrace()
virtual void InitializeState(size_t num_qubits, AER::Vector< std::complex< double > > &litudes)=0
Initializes the state.
virtual double FidelityWithStatevector(const Eigen::VectorXcd &) const
virtual SimulationType GetSimulationType() const =0
Returns the type of simulation.
virtual void InitializeToBasisState(size_t num_qubits, Types::qubit_t basisState)
Initializes the state to a computational basis state.
void ApplyPhaseDamping(Types::qubit_t qubit, double gamma)
Phase damping with coherence multiplier sqrt(1-gamma).
virtual std::vector< bool > MeasureMany(const Types::qubits_vector &qubits)=0
Performs a measurement on the specified qubits.
virtual void ApplyQuantumChannel(const Types::qubits_vector &targets, const QuantumChannel &channel)
Applies a local CPTP quantum channel to one or two qubits.
virtual long long int GetGatesCounter() const
Returns the gates counter.
void RegisterObserver(const std::shared_ptr< ISimulatorObserver > &observer)
Registers an observer.
void ApplyCorrelatedPhaseFlipNoise(Types::qubit_t qubit0, Types::qubit_t qubit1, double probability, double correlation)
virtual void RestoreInternalDestructiveSavedState()=0
Restores the state from the internally saved state.
virtual void SetMultithreading(bool multithreading=true)=0
Enable/disable multithreading.
virtual void SetGatesCounter(long long int)
Sets the gates counter.
virtual double ExpectationValue(const std::string &pauliString)=0
Returns the expected value of a Pauli string.
void ApplyBitPhaseFlipNoise(Types::qubit_t qubit, double probability)
virtual std::complex< double > Amplitude(Types::qubit_t outcome)=0
Returns the amplitude of the specified state.
virtual size_t GetNumberOfQubits() const =0
Returns the number of qubits.
virtual void InitializeToMixtureOfBasisStates(size_t num_qubits, const std::vector< std::pair< std::vector< bool >, double > > &mixture)
Initializes the state to a classical mixture of computational basis states.
virtual SimulatorType GetType() const =0
Returns the type of simulator.
void ApplyDephasingNoise(Types::qubit_t qubit, double probability)
noise.h dephasing is a stochastic phase flip, not phase damping.
virtual std::complex< double > AmplitudeRaw(Types::qubit_t outcome)=0
Gets the amplitude.
virtual size_t GetCurrentMaxBondDimension() const
Returns the maximum bond dimension reached.
virtual void SetUseOptimalMeetingPosition(bool)
Enables or disables optimal meeting position for MPS swaps.
virtual void HermitizeDensityMatrix()
virtual void Flush()=0
Flushes the applied operations.
virtual std::complex< double > DensityMatrixTraceOfSquare() const
virtual std::unordered_map< std::vector< bool >, Types::qubit_t > SampleCountsMany(const Types::qubits_vector &qubits, size_t shots=1000)=0
Returns the counts of the outcomes of measurement of the specified qubits, for repeated measurements.
virtual void Configure(const char *key, const char *value)=0
Configures the state.
virtual void Clear()=0
Clears the state.
virtual const std::unordered_map< std::string, std::string > & GetConfigMap() const =0
void ApplyPauliChannel(const Types::qubits_vector &targets, const std::vector< double > &probabilities)
Apply an arbitrary one- or two-qubit Pauli channel.
void ClearObservers()
Clears all observers.
void NotifyObservers(const Types::qubits_vector &affectedQubits)
Notifies observers.
virtual void SetLookaheadDepth(int)
Sets the lookahead depth for swap optimization.
virtual bool GetMultithreading() const =0
Get the multithreading flag.
virtual void SetLookaheadDepthWithHeuristic(int)
Sets the lookahead depth for swap optimization.
virtual bool SupportsQuantumChannels() const
Returns whether this state retains channel ensembles directly.
void ApplyT1RelaxationFromTime(Types::qubit_t qubit, double duration, double t1)
Exact T1 relaxation for a physical duration and time constant.
virtual size_t Measure(const Types::qubits_vector &qubits)=0
Performs a measurement on the specified qubits.
virtual std::vector< bool > MeasureNoCollapseMany()=0
Measures all the qubits without collapsing the state.
virtual ~IState()=default
Virtual destructor.
void ApplyChannel(const Types::qubits_vector &targets, const QuantumChannel::KrausOperators &krausOperators)
Alias matching the channel terminology used by QCSim's dense backend.
void ApplyGeneralizedAmplitudeDamping(Types::qubit_t qubit, double gamma, double excitedStatePopulation)
void ApplyThermalRelaxation(Types::qubit_t qubit, double duration, double t1, double t2, double excitedStatePopulation=0.0)
virtual std::complex< double > DensityMatrixTrace() const
Mixed-state diagnostics.
virtual void IncrementGatesCounter()
Increments the gates counter.
virtual void InitializeState(size_t num_qubits, Eigen::VectorXcd &litudes)=0
Initializes the state.
void ApplyDepolarizingMixingNoise(Types::qubit_t qubit, double mixingProbability)
Depolarizing replacement (1-p)rho + p I/2, fully mixed at p=1.
virtual bool SupportsMPSSwapOptimization() const
Returns if the simulator supports MPS swap optimization.
virtual void TrimMatrixProductOperator()
virtual double getGrowthFactorGate() const
virtual Eigen::MatrixXcd PartialTrace(const Types::qubits_vector &) const
void ApplyAmplitudeDamping(Types::qubit_t qubit, double gamma)
virtual void Reset()=0
Just resets the state to 0.
virtual void InitializeToMixtureOfBasisStates(size_t num_qubits, const std::vector< std::pair< Types::qubit_t, double > > &mixture)
Initializes the state to a classical mixture of computational basis states.
virtual std::string GetConfiguration(const char *key) const =0
Returns configuration value.
static uint64_t DeriveSeed(uint64_t seed, uint64_t stream)
virtual std::unordered_map< Types::qubit_t, Types::qubit_t > SampleCounts(const Types::qubits_vector &qubits, size_t shots=1000)=0
Returns the counts of the outcomes of measurement of the specified qubits, for repeated measurements.
virtual void setGrowthFactorSwap(double factor)
virtual Types::qubit_t MeasureNoCollapse()=0
Measures all the qubits without collapsing the state.
A local completely-positive, trace-preserving map in Kraus form.
static QuantumChannel Depolarizing(double errorProbability)
Depolarizing channel in the NoiseModel/QCSim convention: (1-p)rho + p/3 (XrhoX + YrhoY + ZrhoZ).
static QuantumChannel PhaseDamping(double gamma)
Phase damping with coherence multiplier sqrt(1-gamma).
static QuantumChannel Pauli(double px, double py, double pz)
Single-qubit Pauli channel with X, Y and Z error probabilities.
static QuantumChannel AmplitudeDamping(double gamma)
|1> -> |0> relaxation with probability gamma.
static QuantumChannel GeneralizedAmplitudeDamping(double gamma, double excitedStatePopulation)
Finite-temperature amplitude damping.
static QuantumChannel DepolarizingMixing(double mixingProbability)
Depolarizing channel in the replacement convention: (1-p)rho + p I/2.
static QuantumChannel CorrelatedPhaseFlip(double probability)
(1-p)rho + p (Z (x) Z) rho (Z (x) Z).
static QuantumChannel BitPhaseFlip(double probability)
(1-p) rho + p Y rho Y.
static QuantumChannel ThermalRelaxation(double duration, double t1, double t2, double excitedStatePopulation=0.0)
Hardware-style thermal relaxation for a duration, T1 and T2.
static QuantumChannel TwoQubitDepolarizingMixing(double mixingProbability)
Two-qubit replacement depolarizing, fully mixed at probability one.
static QuantumChannel PhaseFlip(double probability)
(1-p) rho + p Z rho Z.
static QuantumChannel TwoQubitDepolarizing(double errorProbability)
Two-qubit depolarizing in the total-Pauli-error convention: identity has probability 1-p and each oth...
std::vector< Matrix > KrausOperators
static QuantumChannel BitFlip(double probability)
(1-p) rho + p X rho X.
SimulationType
The type of simulation.
@ kExtendedStabilizer
Extended stabilizer simulation type.
@ kOther
other simulation type, could occur for the aer simulator, which also has unitary and superop methods
@ kStatevector
statevector simulation type
@ kMatrixProductState
matrix product state simulation type
@ kDensityMatrix
Density matrix simulation type.
@ kStabilizer
Clifford gates simulation type.
@ kMatrixProductOperator
Matrix product operator simulation type.
@ kPauliPropagator
Pauli propagator simulation type.
@ kTensorNetwork
Tensor network simulation type.
@ kPathIntegral
Path integral simulation type.
SimulatorType
The type of simulator.
@ kCompositeQCSim
composite qcsim simulator type
@ kQCSim
qcsim simulator type
@ kQiskitAer
qiskit aer simulator type
@ kQuestSim
quest simulator type
@ kDistMpiGpuSim
state distributed across MPI ranks/GPUs
@ kCompositeQiskitAer
composite qiskit aer simulator type
@ kDistGpuSim
state distributed across local GPUs
@ kGpuSim
gpu simulator type
bool IsDistributedGpuSimulator(SimulatorType type)
bool IsGpuSimulator(SimulatorType type)
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.
uint_fast64_t qubit_t
The type of a qubit.