Maestro 0.2.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 <complex>
26#include <unordered_map>
27#include <unordered_set>
28#include <vector>
29
30#ifndef NO_QISKIT_AER
31#include "framework/linalg/vector.hpp"
32#endif
33
34#include "SimulatorObserver.h"
35
36namespace Circuits {
37template <typename Time> class IOperation;
38}
39
40namespace Simulators {
41
54/*
55template<class T> class avoid_init_allocator : public std::allocator<T>
56{
57public:
58 using std::allocator<T>::allocator;
59
60 template <class U, class... Args> void construct(U*, Args&&...) {}
61};
62*/
63
68enum class SimulatorType : int {
69#ifndef NO_QISKIT_AER
71#endif
73#ifndef NO_QISKIT_AER
75#endif
79};
80
96
104class IState {
105 public:
111 virtual ~IState() = default;
112
120 virtual void Initialize() = 0;
121
134 virtual void InitializeState(
135 size_t num_qubits, std::vector<std::complex<double>> &amplitudes) = 0;
136
149#ifndef NO_QISKIT_AER
150 virtual void InitializeState(
151 size_t num_qubits, AER::Vector<std::complex<double>> &amplitudes) = 0;
152#endif
153
166 virtual void InitializeState(size_t num_qubits,
167 Eigen::VectorXcd &amplitudes) = 0;
168
175 virtual void Reset() = 0;
176
184 virtual bool SupportsMPSSwapOptimization() const { return false; }
185
195 const std::vector<long long int> &initialMap) {}
196
205 virtual void SetUseOptimalMeetingPosition(bool /*enable*/) {}
206
215 virtual void SetLookaheadDepth(int /*depth*/) {}
216
226 virtual void SetLookaheadDepthWithHeuristic(int /*depth*/) {}
227
235 virtual void SetUpcomingGates(
236 const std::vector<std::shared_ptr<Circuits::IOperation<double>>> & /*gates*/) {}
237
245 virtual long long int GetGatesCounter() const { return 0; }
246
254 virtual void SetGatesCounter(long long int /*counter*/) {}
255
262 virtual void IncrementGatesCounter() {}
263
264 // the following four functions are also for MPS swaps optimizations, might be removed in the future
265
266 virtual double getGrowthFactorSwap() const { return 0.; }
267 virtual double getGrowthFactorGate() const { return 0.; }
268
269 virtual void setGrowthFactorSwap(double factor) { }
270 virtual void setGrowthFactorGate(double factor) { }
271
281 virtual void Configure(const char *key, const char *value) = 0;
282
290 virtual std::string GetConfiguration(const char *key) const = 0;
291
299 virtual size_t AllocateQubits(size_t num_qubits) = 0;
300
307 virtual size_t GetNumberOfQubits() const = 0;
308
316 virtual void Clear() = 0;
317
328 virtual size_t Measure(const Types::qubits_vector &qubits) = 0;
329
336 virtual std::vector<bool> MeasureMany(const Types::qubits_vector &qubits) = 0;
337
344 virtual void ApplyReset(const Types::qubits_vector &qubits) = 0;
345
357 virtual double Probability(Types::qubit_t outcome) = 0;
358
369 virtual std::complex<double> Amplitude(Types::qubit_t outcome) = 0;
370
384 virtual std::complex<double> ProjectOnZero() = 0;
385
396 virtual std::vector<double> AllProbabilities() = 0;
397
409 virtual std::vector<double> Probabilities(
410 const Types::qubits_vector &qubits) = 0;
411
428 virtual std::unordered_map<Types::qubit_t, Types::qubit_t> SampleCounts(
429 const Types::qubits_vector &qubits, size_t shots = 1000) = 0;
430
444 virtual std::unordered_map<std::vector<bool>, Types::qubit_t>
445 SampleCountsMany(const Types::qubits_vector &qubits, size_t shots = 1000) = 0;
446
458 virtual double ExpectationValue(const std::string &pauliString) = 0;
459
468 void RegisterObserver(const std::shared_ptr<ISimulatorObserver> &observer) {
469 observers.insert(observer);
470 }
471
480 void UnregisterObserver(const std::shared_ptr<ISimulatorObserver> &observer) {
481 observers.erase(observer);
482 }
483
489 void ClearObservers() { observers.clear(); }
490
499 virtual SimulatorType GetType() const = 0;
500
510
518 virtual void Flush() = 0;
519
530
538
548 virtual void SaveState() = 0;
549
558 virtual void RestoreState() = 0;
559
567 virtual std::complex<double> AmplitudeRaw(Types::qubit_t outcome) = 0;
568
577 virtual void SetMultithreading(bool multithreading = true) = 0;
578
586 virtual bool GetMultithreading() const = 0;
587
598 virtual bool IsQcsim() const = 0;
599
617
632 virtual std::vector<bool> MeasureNoCollapseMany() = 0;
633
634 protected:
640 void DontNotify() { notifyObservers = false; }
641
647 void Notify() { notifyObservers = true; }
648
656 void NotifyObservers(const Types::qubits_vector &affectedQubits) {
657 if (!notifyObservers) return;
658
659 for (auto &observer : observers) {
660 observer->Update(affectedQubits);
661 }
662 }
663
664 private:
665 std::unordered_set<std::shared_ptr<ISimulatorObserver>>
666 observers;
667 bool notifyObservers =
668 true;
669};
670
671} // namespace Simulators
672
673#endif // !_SIMULATOR_STATE_H_
The operation interface.
Definition Operations.h:358
Interface class for a quantum computing simulator state.
Definition State.h:104
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 Notify()
Starts notifying observers.
Definition State.h:647
virtual double getGrowthFactorSwap() const
Definition State.h:266
virtual std::vector< double > Probabilities(const Types::qubits_vector &qubits)=0
Returns the probabilities of the specified outcomes.
virtual void SetInitialQubitsMap(const std::vector< long long int > &initialMap)
Sets the initial qubits map, if possible.
Definition State.h:194
virtual void SetUpcomingGates(const std::vector< std::shared_ptr< Circuits::IOperation< double > > > &)
Supplies upcoming gates for lookahead swap optimization.
Definition State.h:235
void UnregisterObserver(const std::shared_ptr< ISimulatorObserver > &observer)
Unregisters an observer.
Definition State.h:480
virtual void RestoreState()=0
Restores the state from the internally saved state.
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 Probability(Types::qubit_t outcome)=0
Returns the probability of the specified outcome.
virtual void Initialize()=0
Initializes the state.
virtual size_t AllocateQubits(size_t num_qubits)=0
Allocates qubits.
void DontNotify()
Stops notifying observers.
Definition State.h:640
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:270
virtual std::vector< double > AllProbabilities()=0
Returns the probabilities of all possible outcomes.
virtual void InitializeState(size_t num_qubits, AER::Vector< std::complex< double > > &amplitudes)=0
Initializes the state.
virtual SimulationType GetSimulationType() const =0
Returns the type of simulation.
virtual std::vector< bool > MeasureMany(const Types::qubits_vector &qubits)=0
Performs a measurement on the specified qubits.
virtual long long int GetGatesCounter() const
Returns the gates counter.
Definition State.h:245
void RegisterObserver(const std::shared_ptr< ISimulatorObserver > &observer)
Registers an observer.
Definition State.h:468
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:254
virtual double ExpectationValue(const std::string &pauliString)=0
Returns the expected value of a Pauli string.
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 SimulatorType GetType() const =0
Returns the type of simulator.
virtual std::complex< double > AmplitudeRaw(Types::qubit_t outcome)=0
Gets the amplitude.
virtual void SetUseOptimalMeetingPosition(bool)
Enables or disables optimal meeting position for MPS swaps.
Definition State.h:205
virtual void Flush()=0
Flushes the applied operations.
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.
void ClearObservers()
Clears all observers.
Definition State.h:489
void NotifyObservers(const Types::qubits_vector &affectedQubits)
Notifies observers.
Definition State.h:656
virtual void SetLookaheadDepth(int)
Sets the lookahead depth for swap optimization.
Definition State.h:215
virtual bool GetMultithreading() const =0
Get the multithreading flag.
virtual void SetLookaheadDepthWithHeuristic(int)
Sets the lookahead depth for swap optimization.
Definition State.h:226
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.
virtual void IncrementGatesCounter()
Increments the gates counter.
Definition State.h:262
virtual void InitializeState(size_t num_qubits, Eigen::VectorXcd &amplitudes)=0
Initializes the state.
virtual bool SupportsMPSSwapOptimization() const
Returns if the simulator supports MPS swap optimization.
Definition State.h:184
virtual double getGrowthFactorGate() const
Definition State.h:267
virtual void Reset()=0
Just resets the state to 0.
virtual std::string GetConfiguration(const char *key) const =0
Returns configuration value.
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:269
virtual Types::qubit_t MeasureNoCollapse()=0
Measures all the qubits without collapsing the state.
SimulationType
The type of simulation.
Definition State.h:85
@ kExtendedStabilizer
Extended stabilizer simulation type.
Definition State.h:91
@ kOther
other simulation type, could occur for the aer simulator, which also has density matrix,...
Definition State.h:93
@ kStatevector
statevector simulation type
Definition State.h:86
@ kMatrixProductState
matrix product state simulation type
Definition State.h:87
@ kStabilizer
Clifford gates simulation type.
Definition State.h:88
@ kPauliPropagator
Pauli propagator simulation type.
Definition State.h:90
@ kTensorNetwork
Tensor network simulation type.
Definition State.h:89
@ kPathIntegral
Path integral simulation type.
Definition State.h:92
SimulatorType
The type of simulator.
Definition State.h:68
@ kCompositeQCSim
composite qcsim simulator type
Definition State.h:76
@ kQCSim
qcsim simulator type
Definition State.h:72
@ kQiskitAer
qiskit aer simulator type
Definition State.h:70
@ kQuestSim
quest simulator type
Definition State.h:78
@ kCompositeQiskitAer
composite qiskit aer simulator type
Definition State.h:74
@ kGpuSim
gpu simulator type
Definition State.h:77
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.
Definition Types.h:22
uint_fast64_t qubit_t
The type of a qubit.
Definition Types.h:21