Maestro 0.3.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
State.h
Go to the documentation of this file.
1
18
19#pragma once
20
21#ifndef _SIMULATOR_STATE_H_
22#define _SIMULATOR_STATE_H_
23
24#include <Eigen/Eigen>
25#include <cmath>
26#include <complex>
27#include <cstdint>
28#include <random>
29#include <stdexcept>
30#include <unordered_map>
31#include <unordered_set>
32#include <utility>
33#include <vector>
34
35#ifndef NO_QISKIT_AER
36#include "framework/linalg/vector.hpp"
37#endif
38
42#include "SimulatorObserver.h"
43
44namespace Circuits
45{
46template <typename Time> class IOperation;
47}
48
49namespace Simulators
50{
51
64/*
65template<class T> class avoid_init_allocator : public std::allocator<T>
66{
67public:
68 using std::allocator<T>::allocator;
69
70 template <class U, class... Args> void construct(U*, Args&&...) {}
71};
72*/
73
78enum class SimulatorType : int
79{
80#ifndef NO_QISKIT_AER
82#endif
84#ifndef NO_QISKIT_AER
86#endif
92};
93
98
100{
102}
103
122
131{
132 public:
138 virtual ~IState() = default;
139
141 virtual void SetSeed(uint64_t seed)
142 {
143 SeedAuxiliaryRng(seed);
144 const std::string value = std::to_string(seed);
145 Configure("seed", value.c_str());
146 }
147
158 {
159 return auxUniform(auxRng);
160 }
161
162 static uint64_t DeriveSeed(uint64_t seed, uint64_t stream)
163 {
164 return Utils::RandomStream::Derive(seed, stream);
165 }
166
174 virtual void Initialize() = 0;
175
188 virtual void InitializeState(size_t num_qubits, std::vector<std::complex<double>> &amplitudes) = 0;
189
202#ifndef NO_QISKIT_AER
203 virtual void InitializeState(size_t num_qubits, AER::Vector<std::complex<double>> &amplitudes) = 0;
204#endif
205
218 virtual void InitializeState(size_t num_qubits, Eigen::VectorXcd &amplitudes) = 0;
219
237 virtual void InitializeToBasisState(size_t num_qubits, Types::qubit_t basisState)
238 {
239 (void)num_qubits;
240 (void)basisState;
241 throw std::runtime_error("This simulator does not support initialization to a computational "
242 "basis state");
243 }
244
254 virtual void InitializeToBasisState(size_t num_qubits, const std::vector<bool> &basisState)
255 {
256 (void)num_qubits;
257 (void)basisState;
258 throw std::runtime_error("This simulator does not support initialization to a computational "
259 "basis state");
260 }
261
281 virtual void InitializeToMixtureOfBasisStates(size_t num_qubits, const std::vector<std::pair<Types::qubit_t, double>> &mixture)
282 {
283 (void)num_qubits;
284 (void)mixture;
285 throw std::runtime_error("This simulator does not support initialization to a mixture of "
286 "computational basis states");
287 }
288
300 virtual void InitializeToMixtureOfBasisStates(size_t num_qubits, const std::vector<std::pair<std::vector<bool>, double>> &mixture)
301 {
302 (void)num_qubits;
303 (void)mixture;
304 throw std::runtime_error("This simulator does not support initialization to a mixture of "
305 "computational basis states");
306 }
307
314 virtual void Reset() = 0;
315
323 virtual bool SupportsMPSSwapOptimization() const
324 {
325 return false;
326 }
327
328 // Named SWAP gates exchange logical labels without moving tensor sites.
329 // Internal routing swaps still require tensor contractions.
330 virtual bool SwapGatesRelabelQubits() const
331 {
332 return false;
333 }
334
335 // True when external meeting-position lookahead can use an upcoming list.
336 // Requires a live MPS/MPO, enabled lookahead and an authoritative qubit map.
337 virtual bool IsRoutingLookaheadEnabled() const
338 {
339 return false;
340 }
341
350 virtual void SetInitialQubitsMap(const std::vector<long long int> &initialMap)
351 {
352 }
353
362 virtual void SetUseOptimalMeetingPosition(bool /*enable*/)
363 {
364 }
365
374 virtual void SetLookaheadDepth(int /*depth*/)
375 {
376 }
377
378 // Network execution calls this after computing the initial layout and
379 // resolving its automatic depth. Explicit SetLookaheadDepth calls retain
380 // their original meaning, including zero for immediate-cost routing.
381 virtual void SetAutomaticLookaheadDepth(int depth)
382 {
383 SetLookaheadDepth(depth);
384 }
385
395 virtual void SetLookaheadDepthWithHeuristic(int /*depth*/)
396 {
397 }
398
406 virtual void SetUpcomingGates(const std::vector<std::shared_ptr<Circuits::IOperation<double>>> & /*gates*/)
407 {
408 }
409
410 // Prepared interactions used by initial-layout optimization. An active
411 // routing plan may retain its complete indexed window during execution;
412 // this is not a remaining-operations/suffix API. Inactive mid-execution
413 // views are empty. Empty means the caller should retain its circuit view.
414 virtual const std::vector<std::shared_ptr<Circuits::IOperation<double>>> &GetUpcomingRoutingOperations() const
415 {
416 static const std::vector<std::shared_ptr<Circuits::IOperation<double>>> empty;
417 return empty;
418 }
419
428 virtual long long int GetGatesCounter() const
429 {
430 return 0;
431 }
432
442 virtual void SetGatesCounter(long long int /*counter*/)
443 {
444 }
445
455 {
456 }
457
458 // the following four functions are also for MPS swaps optimizations, might be
459 // removed in the future
460
461 virtual double getGrowthFactorSwap() const
462 {
463 return 0.;
464 }
465
466 virtual double getGrowthFactorGate() const
467 {
468 return 0.;
469 }
470
471 virtual void setGrowthFactorSwap(double factor)
472 {
473 }
474
475 virtual void setGrowthFactorGate(double factor)
476 {
477 }
478
488 virtual void Configure(const char *key, const char *value) = 0;
489
497 virtual std::string GetConfiguration(const char *key) const = 0;
498
506 virtual size_t AllocateQubits(size_t num_qubits) = 0;
507
514 virtual size_t GetNumberOfQubits() const = 0;
515
523 virtual void Clear() = 0;
524
535 virtual size_t Measure(const Types::qubits_vector &qubits) = 0;
536
543 virtual std::vector<bool> MeasureMany(const Types::qubits_vector &qubits) = 0;
544
551 virtual void ApplyReset(const Types::qubits_vector &qubits) = 0;
552
561 virtual bool SupportsQuantumChannels() const
562 {
563 return false;
564 }
565
573 virtual void ApplyQuantumChannel(const Types::qubits_vector &targets, const QuantumChannel &channel)
574 {
575 (void)targets;
576 (void)channel;
577 throw std::runtime_error("This simulator does not support exact quantum-channel evolution");
578 }
579
585 virtual double ProbabilityBits(const std::vector<bool> &)
586 {
587 throw std::runtime_error("This simulator does not support ProbabilityBits");
588 }
589
590 virtual std::complex<double> DensityMatrixElementBits(const std::vector<bool> &, const std::vector<bool> &) const
591 {
592 throw std::runtime_error("This simulator does not support DensityMatrixElementBits");
593 }
594
595 virtual Eigen::MatrixXcd GetDensityMatrix(bool = true) const
596 {
597 throw std::runtime_error("This simulator does not support GetDensityMatrix");
598 }
599
600 virtual std::complex<double> ExpectationValueComplex(const std::string &, bool = true) const
601 {
602 throw std::runtime_error("This simulator does not support ExpectationValueComplex");
603 }
604
605 // Same normalization and validation as the scalar complex query. Backends
606 // may share contractions; other backends retain their scalar behavior.
607 virtual std::vector<std::complex<double>> ExpectationValuesComplex(const std::vector<std::string> &paulis, bool normalized = true) const
608 {
609 std::vector<std::complex<double>> values;
610 values.reserve(paulis.size());
611 for (const auto &pauli : paulis)
612 values.push_back(ExpectationValueComplex(pauli, normalized));
613 return values;
614 }
615
616 virtual void ApplyOperator(const Types::qubits_vector &, const Eigen::MatrixXcd &, bool = false)
617 {
618 throw std::runtime_error("This simulator does not support ApplyOperator");
619 }
620
622 {
623 throw std::runtime_error("This simulator does not support MoveAtBeginningOfChain");
624 }
625
628 virtual std::complex<double> DensityMatrixTrace() const
629 {
630 throw std::runtime_error("This simulator does not expose a density-matrix trace");
631 }
632
633 virtual double DensityMatrixPurity() const
634 {
635 throw std::runtime_error("This simulator does not expose density-matrix purity");
636 }
637
638 virtual std::complex<double> DensityMatrixTraceOfSquare() const
639 {
640 throw std::runtime_error("This simulator does not expose Tr(rho^2)");
641 }
642
643 virtual std::complex<double> DensityMatrixOverlap(const IState &) const
644 {
645 throw std::runtime_error("This simulator does not support density-matrix overlap");
646 }
647
648 virtual double DensityMatrixHermiticityResidual() const
649 {
650 throw std::runtime_error("This simulator does not expose a Hermiticity residual");
651 }
652
653 virtual bool IsDensityMatrixHermitian(double = 1e-10) const
654 {
655 throw std::runtime_error("This simulator does not expose a Hermiticity test");
656 }
657
658 virtual Eigen::MatrixXcd PartialTrace(const Types::qubits_vector &) const
659 {
660 throw std::runtime_error("This simulator does not support partial trace");
661 }
662
663 virtual double FidelityWithStatevector(const Eigen::VectorXcd &) const
664 {
665 throw std::runtime_error("This simulator does not support mixed-state fidelity");
666 }
667
669 {
670 throw std::runtime_error("This simulator cannot restore density-matrix trace");
671 }
672
674 {
675 throw std::runtime_error("This simulator cannot hermitize its density matrix");
676 }
677
679 virtual void Trim()
680 {
681 throw std::runtime_error("This simulator is not a matrix-product state or operator");
682 }
683
685 virtual void ReCanonicalize()
686 {
687 throw std::runtime_error("This simulator is not a matrix-product state or operator");
688 }
689
692 {
693 ApplyQuantumChannel(targets, QuantumChannel(krausOperators));
694 }
695
697 void ApplyChannel(const Types::qubits_vector &targets, const QuantumChannel::KrausOperators &krausOperators)
698 {
699 ApplyKrausChannel(targets, krausOperators);
700 }
701
703 void ApplyPauliChannel(const Types::qubits_vector &targets, const std::vector<double> &probabilities)
704 {
705 ApplyQuantumChannel(targets, QuantumChannel::Pauli(probabilities));
706 }
707
709 void ApplyPauliChannel(Types::qubit_t qubit, double px, double py, double pz)
710 {
711 ApplyQuantumChannel({qubit}, QuantumChannel::Pauli(px, py, pz));
712 }
713
714 void ApplyBitFlipNoise(Types::qubit_t qubit, double probability)
715 {
716 ApplyQuantumChannel({qubit}, QuantumChannel::BitFlip(probability));
717 }
718
719 void ApplyBitPhaseFlipNoise(Types::qubit_t qubit, double probability)
720 {
722 }
723
724 void ApplyPhaseFlipNoise(Types::qubit_t qubit, double probability)
725 {
726 ApplyQuantumChannel({qubit}, QuantumChannel::PhaseFlip(probability));
727 }
728
730 void ApplyDephasingNoise(Types::qubit_t qubit, double probability)
731 {
732 ApplyPhaseFlipNoise(qubit, probability);
733 }
734
736 void ApplyDepolarizingNoise(Types::qubit_t qubit, double errorProbability)
737 {
738 ApplyQuantumChannel({qubit}, QuantumChannel::Depolarizing(errorProbability));
739 }
740
742 void ApplyDepolarizingMixingNoise(Types::qubit_t qubit, double mixingProbability)
743 {
744 ApplyQuantumChannel({qubit}, QuantumChannel::DepolarizingMixing(mixingProbability));
745 }
746
747 void ApplyAmplitudeDamping(Types::qubit_t qubit, double gamma)
748 {
750 }
751
753 void ApplyT1Relaxation(Types::qubit_t qubit, double gamma)
754 {
755 ApplyAmplitudeDamping(qubit, gamma);
756 }
757
759 void ApplyT1RelaxationFromTime(Types::qubit_t qubit, double duration, double t1)
760 {
761 if (!std::isfinite(duration) || duration < 0.0)
762 throw std::invalid_argument("T1-relaxation duration must be finite and nonnegative");
763 if (std::isnan(t1) || t1 <= 0.0)
764 throw std::invalid_argument("T1 must be positive (infinity is allowed)");
765 const double gamma = std::isinf(t1) ? 0.0 : -std::expm1(-duration / t1);
766 ApplyAmplitudeDamping(qubit, gamma);
767 }
768
770 void ApplyPhaseDamping(Types::qubit_t qubit, double gamma)
771 {
773 }
774
780 void ApplyPhaseDampingFromTime(Types::qubit_t qubit, double duration, double tPhi)
781 {
782 if (!std::isfinite(duration) || duration < 0.0)
783 throw std::invalid_argument("Phase-damping duration must be finite and nonnegative");
784 if (std::isnan(tPhi) || tPhi <= 0.0)
785 throw std::invalid_argument("T_phi must be positive (infinity is allowed)");
786 const double gamma = std::isinf(tPhi) ? 0.0 : -std::expm1(-2.0 * duration / tPhi);
787 ApplyPhaseDamping(qubit, gamma);
788 }
789
790 void ApplyGeneralizedAmplitudeDamping(Types::qubit_t qubit, double gamma, double excitedStatePopulation)
791 {
792 ApplyQuantumChannel({qubit}, QuantumChannel::GeneralizedAmplitudeDamping(gamma, excitedStatePopulation));
793 }
794
795 void ApplyThermalRelaxation(Types::qubit_t qubit, double duration, double t1, double t2, double excitedStatePopulation = 0.0)
796 {
797 ApplyQuantumChannel({qubit}, QuantumChannel::ThermalRelaxation(duration, t1, t2, excitedStatePopulation));
798 }
799
800 void ApplyCorrelatedPhaseFlipNoise(Types::qubit_t qubit0, Types::qubit_t qubit1, double probability)
801 {
802 ApplyQuantumChannel({qubit0, qubit1}, QuantumChannel::CorrelatedPhaseFlip(probability));
803 }
804
805 void ApplyCorrelatedPhaseFlipNoise(Types::qubit_t qubit0, Types::qubit_t qubit1, double probability, double correlation)
806 {
807 ApplyQuantumChannel({qubit0, qubit1}, QuantumChannel::CorrelatedPhaseFlip(probability, correlation));
808 }
809
810 void ApplyTwoQubitDepolarizingNoise(Types::qubit_t qubit0, Types::qubit_t qubit1, double errorProbability)
811 {
812 ApplyQuantumChannel({qubit0, qubit1}, QuantumChannel::TwoQubitDepolarizing(errorProbability));
813 }
814
815 void ApplyTwoQubitDepolarizingMixingNoise(Types::qubit_t qubit0, Types::qubit_t qubit1, double mixingProbability)
816 {
817 ApplyQuantumChannel({qubit0, qubit1}, QuantumChannel::TwoQubitDepolarizingMixing(mixingProbability));
818 }
819
831 virtual double Probability(Types::qubit_t outcome) = 0;
832
843 virtual std::complex<double> Amplitude(Types::qubit_t outcome) = 0;
844
845 // Full pure-state readout, indexed by logical qubit (q0 is the low bit).
846 // Backends with a bulk reader override the scalar fallback.
847 virtual std::vector<std::complex<double>> GetStateVector()
848 {
850 throw std::runtime_error("Mixed states have no unique statevector");
851 if (GetNumberOfQubits() == 0)
852 return {};
853 std::vector<std::complex<double>> result(TensorQueries::DenseSize(GetNumberOfQubits()));
854 for (size_t i = 0; i < result.size(); ++i)
855 result[i] = Amplitude(i);
856 return result;
857 }
858
859 // <psi|O_(k-1)...O_0|psi>. Each matrix is 2x2; repeated targets are allowed.
860 // This is a read-only query, including for non-Hermitian/non-unitary
861 // operators.
862 virtual std::complex<double> ExpectationValueOperators(const Types::qubits_vector &, const std::vector<Eigen::MatrixXcd> &)
863 {
864 throw std::runtime_error("This simulator does not support ExpectationValueOperators");
865 }
866
880 virtual std::complex<double> ProjectOnZero() = 0;
881
892 virtual std::vector<double> AllProbabilities() = 0;
893
905 virtual std::vector<double> Probabilities(const Types::qubits_vector &qubits) = 0;
906
923 virtual std::unordered_map<Types::qubit_t, Types::qubit_t> SampleCounts(const Types::qubits_vector &qubits, size_t shots = 1000) = 0;
924
938 virtual std::unordered_map<std::vector<bool>, Types::qubit_t> SampleCountsMany(const Types::qubits_vector &qubits, size_t shots = 1000) = 0;
939
951 virtual double ExpectationValue(const std::string &pauliString) = 0;
952
953 // Backends may share contractions across this read-only batch. The default
954 // retains the existing behavior for every other simulator.
955 virtual std::vector<double> ExpectationValues(const std::vector<std::string> &paulis)
956 {
957 std::vector<double> values;
958 values.reserve(paulis.size());
959 for (const auto &pauli : paulis)
960 values.push_back(ExpectationValue(pauli));
961 return values;
962 }
963
972 void RegisterObserver(const std::shared_ptr<ISimulatorObserver> &observer)
973 {
974 observers.insert(observer);
975 }
976
985 void UnregisterObserver(const std::shared_ptr<ISimulatorObserver> &observer)
986 {
987 observers.erase(observer);
988 }
989
996 {
997 observers.clear();
998 }
999
1008 virtual SimulatorType GetType() const = 0;
1009
1019
1027 virtual void Flush() = 0;
1028
1039
1047
1057 virtual void SaveState() = 0;
1058
1067 virtual void RestoreState() = 0;
1068
1076 virtual std::complex<double> AmplitudeRaw(Types::qubit_t outcome) = 0;
1077
1086 virtual void SetMultithreading(bool multithreading = true) = 0;
1087
1095 virtual bool GetMultithreading() const = 0;
1096
1107 virtual bool IsQcsim() const = 0;
1108
1126
1141 virtual std::vector<bool> MeasureNoCollapseMany() = 0;
1142
1149 virtual size_t GetCurrentMaxBondDimension() const
1150 {
1151 return 0;
1152 }
1153
1154 // Internal telemetry must not synchronize a pending gate after every call.
1155 virtual size_t GetExecutedMaxBondDimension() const
1156 {
1158 }
1159
1160 virtual const std::unordered_map<std::string, std::string> &GetConfigMap() const = 0;
1161
1162 protected:
1164 void SeedAuxiliaryRng(uint64_t seed)
1165 {
1166 auxRng.seed(DeriveSeed(seed, kAuxRngStream));
1167 auxUniform.reset();
1168 }
1169
1176 {
1177 notifyObservers = false;
1178 }
1179
1185 void Notify()
1186 {
1187 notifyObservers = true;
1188 }
1189
1197 void NotifyObservers(const Types::qubits_vector &affectedQubits)
1198 {
1199 if (!notifyObservers)
1200 return;
1201
1202 for (auto &observer : observers)
1203 {
1204 observer->Update(affectedQubits);
1205 }
1206 }
1207
1208 protected:
1209 bool NotificationsEnabled() const noexcept
1210 {
1211 return notifyObservers;
1212 }
1213
1214 private:
1217 static constexpr uint64_t kAuxRngStream = 0x52454144ULL; // 'READ'
1218
1219 std::unordered_set<std::shared_ptr<ISimulatorObserver>> observers;
1220 bool notifyObservers = true;
1221 std::mt19937_64 auxRng{std::random_device{}()};
1222 std::uniform_real_distribution<double> auxUniform{0.0, 1.0};
1223};
1224
1225} // namespace Simulators
1226
1227#endif // !_SIMULATOR_STATE_H_
Backend-independent constructors for local CPTP quantum channels.
The operation interface.
Definition Operations.h:402
Interface class for a quantum computing simulator state.
Definition State.h:131
bool NotificationsEnabled() const noexcept
Definition State.h:1209
double RandomUniform()
Uniform random number in [0, 1) from the auxiliary stream.
Definition State.h:157
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 double ProbabilityBits(const std::vector< bool > &)
MPO extensions: bit vectors are q0-first; matrices use logical basis order.
Definition State.h:585
virtual std::complex< double > ExpectationValueComplex(const std::string &, bool=true) const
Definition State.h:600
virtual void SaveStateToInternalDestructive()=0
Saves the state to internal storage.
void ApplyTwoQubitDepolarizingNoise(Types::qubit_t qubit0, Types::qubit_t qubit1, double errorProbability)
Definition State.h:810
virtual void SetAutomaticLookaheadDepth(int depth)
Definition State.h:381
void Notify()
Starts notifying observers.
Definition State.h:1185
virtual double getGrowthFactorSwap() const
Definition State.h:461
virtual void SetSeed(uint64_t seed)
Seed every random stream owned by this simulator.
Definition State.h:141
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)
Definition State.h:815
virtual std::vector< std::complex< double > > ExpectationValuesComplex(const std::vector< std::string > &paulis, bool normalized=true) const
Definition State.h:607
void ApplyPauliChannel(Types::qubit_t qubit, double px, double py, double pz)
Apply a single-qubit Pauli channel specified by X/Y/Z probabilities.
Definition State.h:709
virtual std::complex< double > ExpectationValueOperators(const Types::qubits_vector &, const std::vector< Eigen::MatrixXcd > &)
Definition State.h:862
virtual void SetInitialQubitsMap(const std::vector< long long int > &initialMap)
Sets the initial qubits map, if possible.
Definition State.h:350
virtual bool IsDensityMatrixHermitian(double=1e-10) const
Definition State.h:653
virtual void SetUpcomingGates(const std::vector< std::shared_ptr< Circuits::IOperation< double > > > &)
Supplies upcoming gates for lookahead swap optimization.
Definition State.h:406
void ApplyPhaseDampingFromTime(Types::qubit_t qubit, double duration, double tPhi)
Pure phase damping for a physical duration and T_phi.
Definition State.h:780
void UnregisterObserver(const std::shared_ptr< ISimulatorObserver > &observer)
Unregisters an observer.
Definition State.h:985
virtual void InitializeToBasisState(size_t num_qubits, const std::vector< bool > &basisState)
Initializes the state to a computational basis state.
Definition State.h:254
virtual Eigen::MatrixXcd GetDensityMatrix(bool=true) const
Definition State.h:595
virtual void RestoreState()=0
Restores the state from the internally saved state.
void ApplyCorrelatedPhaseFlipNoise(Types::qubit_t qubit0, Types::qubit_t qubit1, double probability)
Definition State.h:800
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
Definition State.h:633
virtual std::complex< double > DensityMatrixOverlap(const IState &) const
Definition State.h:643
void ApplyKrausChannel(const Types::qubits_vector &targets, const QuantumChannel::KrausOperators &krausOperators)
Apply an arbitrary CPTP map supplied in Kraus form.
Definition State.h:691
virtual double Probability(Types::qubit_t outcome)=0
Returns the probability of the specified outcome.
virtual double DensityMatrixHermiticityResidual() const
Definition State.h:648
virtual void Initialize()=0
Initializes the state.
void ApplyPhaseFlipNoise(Types::qubit_t qubit, double probability)
Definition State.h:724
virtual size_t AllocateQubits(size_t num_qubits)=0
Allocates qubits.
virtual void Trim()
Compress a matrix-product state or operator to its configured limits.
Definition State.h:679
void DontNotify()
Stops notifying observers.
Definition State.h:1175
void ApplyBitFlipNoise(Types::qubit_t qubit, double probability)
Definition State.h:714
void ApplyDepolarizingNoise(Types::qubit_t qubit, double errorProbability)
Depolarizing noise using total nonidentity-Pauli probability p.
Definition State.h:736
virtual void InitializeState(size_t num_qubits, std::vector< std::complex< double > > &amplitudes)=0
Initializes the state.
virtual void setGrowthFactorGate(double factor)
Definition State.h:475
void ApplyT1Relaxation(Types::qubit_t qubit, double gamma)
Alias for the exact T1 amplitude-damping channel.
Definition State.h:753
virtual std::vector< std::complex< double > > GetStateVector()
Definition State.h:847
virtual std::vector< double > AllProbabilities()=0
Returns the probabilities of all possible outcomes.
virtual void RestoreDensityMatrixTrace()
Definition State.h:668
virtual void InitializeState(size_t num_qubits, AER::Vector< std::complex< double > > &amplitudes)=0
Initializes the state.
void SeedAuxiliaryRng(uint64_t seed)
Shared by SetSeed and backend Configure("seed") implementations.
Definition State.h:1164
virtual double FidelityWithStatevector(const Eigen::VectorXcd &) const
Definition State.h:663
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.
Definition State.h:237
void ApplyPhaseDamping(Types::qubit_t qubit, double gamma)
Phase damping with coherence multiplier sqrt(1-gamma).
Definition State.h:770
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.
Definition State.h:573
virtual long long int GetGatesCounter() const
Returns the gates counter.
Definition State.h:428
void RegisterObserver(const std::shared_ptr< ISimulatorObserver > &observer)
Registers an observer.
Definition State.h:972
void ApplyCorrelatedPhaseFlipNoise(Types::qubit_t qubit0, Types::qubit_t qubit1, double probability, double correlation)
Definition State.h:805
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.
Definition State.h:442
virtual double ExpectationValue(const std::string &pauliString)=0
Returns the expected value of a Pauli string.
void ApplyBitPhaseFlipNoise(Types::qubit_t qubit, double probability)
Definition State.h:719
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.
Definition State.h:300
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.
Definition State.h:730
virtual std::complex< double > AmplitudeRaw(Types::qubit_t outcome)=0
Gets the amplitude.
virtual size_t GetCurrentMaxBondDimension() const
Returns the maximum bond dimension reached.
Definition State.h:1149
virtual void SetUseOptimalMeetingPosition(bool)
Enables or disables optimal meeting position for MPS swaps.
Definition State.h:362
virtual void HermitizeDensityMatrix()
Definition State.h:673
virtual void Flush()=0
Flushes the applied operations.
virtual std::complex< double > DensityMatrixTraceOfSquare() const
Definition State.h:638
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 MoveAtBeginningOfChain(const Types::qubits_vector &)
Definition State.h:621
virtual void Configure(const char *key, const char *value)=0
Configures the state.
virtual bool IsRoutingLookaheadEnabled() const
Definition State.h:337
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.
Definition State.h:703
void ClearObservers()
Clears all observers.
Definition State.h:995
void NotifyObservers(const Types::qubits_vector &affectedQubits)
Notifies observers.
Definition State.h:1197
virtual void SetLookaheadDepth(int)
Sets the lookahead depth for swap optimization.
Definition State.h:374
virtual void ApplyOperator(const Types::qubits_vector &, const Eigen::MatrixXcd &, bool=false)
Definition State.h:616
virtual bool GetMultithreading() const =0
Get the multithreading flag.
virtual void SetLookaheadDepthWithHeuristic(int)
Sets the lookahead depth for swap optimization.
Definition State.h:395
virtual bool SupportsQuantumChannels() const
Returns whether this state retains channel ensembles directly.
Definition State.h:561
void ApplyT1RelaxationFromTime(Types::qubit_t qubit, double duration, double t1)
Exact T1 relaxation for a physical duration and time constant.
Definition State.h:759
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.
Definition State.h:697
void ApplyGeneralizedAmplitudeDamping(Types::qubit_t qubit, double gamma, double excitedStatePopulation)
Definition State.h:790
void ApplyThermalRelaxation(Types::qubit_t qubit, double duration, double t1, double t2, double excitedStatePopulation=0.0)
Definition State.h:795
virtual std::complex< double > DensityMatrixTrace() const
Mixed-state diagnostics.
Definition State.h:628
virtual void IncrementGatesCounter()
Increments the gates counter.
Definition State.h:454
virtual void InitializeState(size_t num_qubits, Eigen::VectorXcd &amplitudes)=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.
Definition State.h:742
virtual size_t GetExecutedMaxBondDimension() const
Definition State.h:1155
virtual bool SupportsMPSSwapOptimization() const
Returns if the simulator supports MPS swap optimization.
Definition State.h:323
virtual double getGrowthFactorGate() const
Definition State.h:466
virtual Eigen::MatrixXcd PartialTrace(const Types::qubits_vector &) const
Definition State.h:658
void ApplyAmplitudeDamping(Types::qubit_t qubit, double gamma)
Definition State.h:747
virtual bool SwapGatesRelabelQubits() const
Definition State.h:330
virtual std::vector< double > ExpectationValues(const std::vector< std::string > &paulis)
Definition State.h:955
virtual void ReCanonicalize()
Restore the canonical form of a matrix-product state or operator.
Definition State.h:685
virtual std::complex< double > DensityMatrixElementBits(const std::vector< bool > &, const std::vector< bool > &) const
Definition State.h:590
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.
Definition State.h:281
virtual std::string GetConfiguration(const char *key) const =0
Returns configuration value.
static uint64_t DeriveSeed(uint64_t seed, uint64_t stream)
Definition State.h:162
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)
Definition State.h:471
virtual const std::vector< std::shared_ptr< Circuits::IOperation< double > > > & GetUpcomingRoutingOperations() const
Definition State.h:414
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.
size_t DenseSize(size_t qubits)
Bounded online routing based on current physical bond dimensions.
SimulationType
The type of simulation.
Definition State.h:109
@ kExtendedStabilizer
Extended stabilizer simulation type.
Definition State.h:115
@ kOther
other simulation type, could occur for the aer simulator, which also has unitary and superop methods
Definition State.h:119
@ kStatevector
statevector simulation type
Definition State.h:110
@ kMatrixProductState
matrix product state simulation type
Definition State.h:111
@ kDensityMatrix
Density matrix simulation type.
Definition State.h:117
@ kStabilizer
Clifford gates simulation type.
Definition State.h:112
@ kMatrixProductOperator
Matrix product operator simulation type.
Definition State.h:118
@ kPauliPropagator
Pauli propagator simulation type.
Definition State.h:114
@ kTensorNetwork
Tensor network simulation type.
Definition State.h:113
@ kPathIntegral
Path integral simulation type.
Definition State.h:116
SimulatorType
The type of simulator.
Definition State.h:79
@ kCompositeQCSim
composite qcsim simulator type
Definition State.h:87
@ kQCSim
qcsim simulator type
Definition State.h:83
@ kQiskitAer
qiskit aer simulator type
Definition State.h:81
@ kQuestSim
quest simulator type
Definition State.h:89
@ kDistMpiGpuSim
state distributed across MPI ranks/GPUs
Definition State.h:91
@ kCompositeQiskitAer
composite qiskit aer simulator type
Definition State.h:85
@ kDistGpuSim
state distributed across local GPUs
Definition State.h:90
@ kGpuSim
gpu simulator type
Definition State.h:88
bool IsDistributedGpuSimulator(SimulatorType type)
Definition State.h:94
bool IsGpuSimulator(SimulatorType type)
Definition State.h:99
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.
Definition Types.h:24
uint_fast64_t qubit_t
The type of a qubit.
Definition Types.h:23
uint64_t Derive(uint64_t seed, uint64_t stream)