Maestro 0.1.0
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
State.h
Go to the documentation of this file.
1
18#pragma once
19
20#ifndef _SIMULATOR_STATE_H_
21#define _SIMULATOR_STATE_H_
22
23#include <Eigen/Eigen>
24#include <complex>
25#include <unordered_map>
26#include <unordered_set>
27#include <vector>
28
29#ifndef NO_QISKIT_AER
30#include "framework/linalg/vector.hpp"
31#endif
32
33#include "SimulatorObserver.h"
34
35namespace Simulators {
36
49/*
50template<class T> class avoid_init_allocator : public std::allocator<T>
51{
52public:
53 using std::allocator<T>::allocator;
54
55 template <class U, class... Args> void construct(U*, Args&&...) {}
56};
57*/
58
63enum class SimulatorType : int {
64#ifndef NO_QISKIT_AER
66#endif
67 kQCSim,
68#ifndef NO_QISKIT_AER
70#endif
72 kGpuSim
73};
74
89
97class IState {
98 public:
104 virtual ~IState() = default;
105
113 virtual void Initialize() = 0;
114
127 virtual void InitializeState(
128 size_t num_qubits, std::vector<std::complex<double>> &amplitudes) = 0;
129
142#ifndef NO_QISKIT_AER
143 virtual void InitializeState(
144 size_t num_qubits, AER::Vector<std::complex<double>> &amplitudes) = 0;
145#endif
146
159 virtual void InitializeState(size_t num_qubits,
160 Eigen::VectorXcd &amplitudes) = 0;
161
168 virtual void Reset() = 0;
169
179 virtual void Configure(const char *key, const char *value) = 0;
180
188 virtual std::string GetConfiguration(const char *key) const = 0;
189
197 virtual size_t AllocateQubits(size_t num_qubits) = 0;
198
205 virtual size_t GetNumberOfQubits() const = 0;
206
214 virtual void Clear() = 0;
215
223 virtual size_t Measure(const Types::qubits_vector &qubits) = 0;
224
231 virtual void ApplyReset(const Types::qubits_vector &qubits) = 0;
232
244 virtual double Probability(Types::qubit_t outcome) = 0;
245
256 virtual std::complex<double> Amplitude(Types::qubit_t outcome) = 0;
257
268 virtual std::vector<double> AllProbabilities() = 0;
269
281 virtual std::vector<double> Probabilities(
282 const Types::qubits_vector &qubits) = 0;
283
297 virtual std::unordered_map<Types::qubit_t, Types::qubit_t> SampleCounts(
298 const Types::qubits_vector &qubits, size_t shots = 1000) = 0;
299
311 virtual double ExpectationValue(const std::string &pauliString) = 0;
312
321 void RegisterObserver(const std::shared_ptr<ISimulatorObserver> &observer) {
322 observers.insert(observer);
323 }
324
333 void UnregisterObserver(const std::shared_ptr<ISimulatorObserver> &observer) {
334 observers.erase(observer);
335 }
336
342 void ClearObservers() { observers.clear(); }
343
352 virtual SimulatorType GetType() const = 0;
353
363
371 virtual void Flush() = 0;
372
383
391
401 virtual void SaveState() = 0;
402
411 virtual void RestoreState() = 0;
412
420 virtual std::complex<double> AmplitudeRaw(Types::qubit_t outcome) = 0;
421
430 virtual void SetMultithreading(bool multithreading = true) = 0;
431
439 virtual bool GetMultithreading() const = 0;
440
451 virtual bool IsQcsim() const = 0;
452
466 virtual Types::qubit_t MeasureNoCollapse() = 0;
467
468 protected:
474 void DontNotify() { notifyObservers = false; }
475
481 void Notify() { notifyObservers = true; }
482
490 void NotifyObservers(const Types::qubits_vector &affectedQubits) {
491 if (!notifyObservers) return;
492
493 for (auto &observer : observers) {
494 observer->Update(affectedQubits);
495 }
496 }
497
498 private:
499 std::unordered_set<std::shared_ptr<ISimulatorObserver>>
500 observers;
501 bool notifyObservers =
502 true;
503};
504
505} // namespace Simulators
506
507#endif // !_SIMULATOR_STATE_H_
Interface class for a quantum computing simulator state.
Definition State.h:97
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:481
virtual std::vector< double > Probabilities(const Types::qubits_vector &qubits)=0
Returns the probabilities of the specified outcomes.
void UnregisterObserver(const std::shared_ptr< ISimulatorObserver > &observer)
Unregisters an observer.
Definition State.h:333
virtual void RestoreState()=0
Restores the state from the internally saved state.
virtual void SaveState()=0
Saves the state to internal storage.
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:474
virtual void InitializeState(size_t num_qubits, std::vector< std::complex< double > > &amplitudes)=0
Initializes the state.
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.
void RegisterObserver(const std::shared_ptr< ISimulatorObserver > &observer)
Registers an observer.
Definition State.h:321
virtual void RestoreInternalDestructiveSavedState()=0
Restores the state from the internally saved state.
virtual void SetMultithreading(bool multithreading=true)=0
Enable/disable multithreading.
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 Flush()=0
Flushes the applied operations.
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:342
void NotifyObservers(const Types::qubits_vector &affectedQubits)
Notifies observers.
Definition State.h:490
virtual bool GetMultithreading() const =0
Get the multithreading flag.
virtual size_t Measure(const Types::qubits_vector &qubits)=0
Performs a measurement on the specified qubits.
virtual ~IState()=default
Virtual destructor.
virtual void InitializeState(size_t num_qubits, Eigen::VectorXcd &amplitudes)=0
Initializes the state.
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 Types::qubit_t MeasureNoCollapse()=0
Measures all the qubits without collapsing the state.
SimulationType
The type of simulation.
Definition State.h:79
@ kExtendedStabilizer
Extended stabilizer simulation type.
@ kOther
other simulation type, could occur for the aer simulator, which also has density matrix,...
@ kStatevector
statevector simulation type
@ kMatrixProductState
matrix product state simulation type
@ kStabilizer
Clifford gates simulation type.
@ kPauliPropagator
Pauli propagator simulation type.
@ kTensorNetwork
Tensor network simulation type.
SimulatorType
The type of simulator.
Definition State.h:63
@ kCompositeQCSim
composite qcsim simulator type
@ kQCSim
qcsim simulator type
@ kQiskitAer
qiskit aer simulator type
@ kCompositeQiskitAer
composite qiskit aer simulator type
@ kGpuSim
gpu simulator type