Maestro 0.1.0
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
State.h
Go to the documentation of this file.
1
17
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
68#ifndef NO_QISKIT_AER
70#endif
72#ifdef __linux__
73 ,
74 kGpuSim
75#endif
76};
77
91
99class IState {
100public:
106 virtual ~IState() = default;
107
115 virtual void Initialize() = 0;
116
129 virtual void
130 InitializeState(size_t num_qubits,
131 std::vector<std::complex<double>> &amplitudes) = 0;
132
145#ifndef NO_QISKIT_AER
146 virtual void
147 InitializeState(size_t num_qubits,
148 AER::Vector<std::complex<double>> &amplitudes) = 0;
149#endif
150
163 virtual void InitializeState(size_t num_qubits,
164 Eigen::VectorXcd &amplitudes) = 0;
165
172 virtual void Reset() = 0;
173
183 virtual void Configure(const char *key, const char *value) = 0;
184
192 virtual std::string GetConfiguration(const char *key) const = 0;
193
201 virtual size_t AllocateQubits(size_t num_qubits) = 0;
202
209 virtual size_t GetNumberOfQubits() const = 0;
210
218 virtual void Clear() = 0;
219
227 virtual size_t Measure(const Types::qubits_vector &qubits) = 0;
228
235 virtual void ApplyReset(const Types::qubits_vector &qubits) = 0;
236
248 virtual double Probability(Types::qubit_t outcome) = 0;
249
260 virtual std::complex<double> Amplitude(Types::qubit_t outcome) = 0;
261
272 virtual std::vector<double> AllProbabilities() = 0;
273
285 virtual std::vector<double>
287
301 virtual std::unordered_map<Types::qubit_t, Types::qubit_t>
302 SampleCounts(const Types::qubits_vector &qubits, size_t shots = 1000) = 0;
303
315 virtual double ExpectationValue(const std::string &pauliString) = 0;
316
325 void RegisterObserver(const std::shared_ptr<ISimulatorObserver> &observer) {
326 observers.insert(observer);
327 }
328
337 void UnregisterObserver(const std::shared_ptr<ISimulatorObserver> &observer) {
338 observers.erase(observer);
339 }
340
346 void ClearObservers() { observers.clear(); }
347
356 virtual SimulatorType GetType() const = 0;
357
367
375 virtual void Flush() = 0;
376
387
395
405 virtual void SaveState() = 0;
406
415 virtual void RestoreState() = 0;
416
424 virtual std::complex<double> AmplitudeRaw(Types::qubit_t outcome) = 0;
425
434 virtual void SetMultithreading(bool multithreading = true) = 0;
435
443 virtual bool GetMultithreading() const = 0;
444
455 virtual bool IsQcsim() const = 0;
456
471
472protected:
478 void DontNotify() { notifyObservers = false; }
479
485 void Notify() { notifyObservers = true; }
486
494 void NotifyObservers(const Types::qubits_vector &affectedQubits) {
495 if (!notifyObservers)
496 return;
497
498 for (auto &observer : observers) {
499 observer->Update(affectedQubits);
500 }
501 }
502
503private:
504 std::unordered_set<std::shared_ptr<ISimulatorObserver>>
505 observers;
506 bool notifyObservers =
507 true;
508};
509
510} // namespace Simulators
511
512#endif // !_SIMULATOR_STATE_H_
Interface class for a quantum computing simulator state.
Definition State.h:99
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:485
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:337
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:478
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:325
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:346
void NotifyObservers(const Types::qubits_vector &affectedQubits)
Notifies observers.
Definition State.h:494
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:82
@ kOther
other simulation type, could occur for the aer simulator, which also has density matrix,...
Definition State.h:87
@ kStatevector
statevector simulation type
Definition State.h:83
@ kMatrixProductState
matrix product state simulation type
Definition State.h:84
@ kStabilizer
Clifford gates simulation type.
Definition State.h:85
@ kTensorNetwork
Tensor network simulation type.
Definition State.h:86
SimulatorType
The type of simulator.
Definition State.h:63
@ kCompositeQCSim
composite qcsim simulator type
Definition State.h:71
@ kQCSim
qcsim simulator type
Definition State.h:67
@ kQiskitAer
qiskit aer simulator type
Definition State.h:65
@ kCompositeQiskitAer
composite qiskit aer simulator type
Definition State.h:69
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.
Definition Types.h:21
uint_fast64_t qubit_t
The type of a qubit.
Definition Types.h:20