Maestro 0.2.5
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
State.h
Go to the documentation of this file.
1
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
72 kQCSim,
73#ifndef NO_QISKIT_AER
75#endif
77 kGpuSim,
79};
80
95
103class IState {
104 public:
110 virtual ~IState() = default;
111
119 virtual void Initialize() = 0;
120
133 virtual void InitializeState(
134 size_t num_qubits, std::vector<std::complex<double>> &amplitudes) = 0;
135
148#ifndef NO_QISKIT_AER
149 virtual void InitializeState(
150 size_t num_qubits, AER::Vector<std::complex<double>> &amplitudes) = 0;
151#endif
152
165 virtual void InitializeState(size_t num_qubits,
166 Eigen::VectorXcd &amplitudes) = 0;
167
174 virtual void Reset() = 0;
175
183 virtual bool SupportsMPSSwapOptimization() const { return false; }
184
194 const std::vector<long long int> &initialMap) {}
195
204 virtual void SetUseOptimalMeetingPosition(bool /*enable*/) {}
205
214 virtual void SetLookaheadDepth(int /*depth*/) {}
215
225 virtual void SetLookaheadDepthWithHeuristic(int /*depth*/) {}
226
234 virtual void SetUpcomingGates(
235 const std::vector<std::shared_ptr<Circuits::IOperation<double>>> & /*gates*/) {}
236
244 virtual long long int GetGatesCounter() const { return 0; }
245
253 virtual void SetGatesCounter(long long int /*counter*/) {}
254
261 virtual void IncrementGatesCounter() {}
262
272 virtual void Configure(const char *key, const char *value) = 0;
273
281 virtual std::string GetConfiguration(const char *key) const = 0;
282
290 virtual size_t AllocateQubits(size_t num_qubits) = 0;
291
298 virtual size_t GetNumberOfQubits() const = 0;
299
307 virtual void Clear() = 0;
308
319 virtual size_t Measure(const Types::qubits_vector &qubits) = 0;
320
327 virtual std::vector<bool> MeasureMany(const Types::qubits_vector &qubits) = 0;
328
335 virtual void ApplyReset(const Types::qubits_vector &qubits) = 0;
336
348 virtual double Probability(Types::qubit_t outcome) = 0;
349
360 virtual std::complex<double> Amplitude(Types::qubit_t outcome) = 0;
361
375 virtual std::complex<double> ProjectOnZero() = 0;
376
387 virtual std::vector<double> AllProbabilities() = 0;
388
400 virtual std::vector<double> Probabilities(
401 const Types::qubits_vector &qubits) = 0;
402
419 virtual std::unordered_map<Types::qubit_t, Types::qubit_t> SampleCounts(
420 const Types::qubits_vector &qubits, size_t shots = 1000) = 0;
421
435 virtual std::unordered_map<std::vector<bool>, Types::qubit_t>
436 SampleCountsMany(const Types::qubits_vector &qubits, size_t shots = 1000) = 0;
437
449 virtual double ExpectationValue(const std::string &pauliString) = 0;
450
459 void RegisterObserver(const std::shared_ptr<ISimulatorObserver> &observer) {
460 observers.insert(observer);
461 }
462
471 void UnregisterObserver(const std::shared_ptr<ISimulatorObserver> &observer) {
472 observers.erase(observer);
473 }
474
480 void ClearObservers() { observers.clear(); }
481
490 virtual SimulatorType GetType() const = 0;
491
501
509 virtual void Flush() = 0;
510
521
529
539 virtual void SaveState() = 0;
540
549 virtual void RestoreState() = 0;
550
558 virtual std::complex<double> AmplitudeRaw(Types::qubit_t outcome) = 0;
559
568 virtual void SetMultithreading(bool multithreading = true) = 0;
569
577 virtual bool GetMultithreading() const = 0;
578
589 virtual bool IsQcsim() const = 0;
590
608
623 virtual std::vector<bool> MeasureNoCollapseMany() = 0;
624
625 protected:
631 void DontNotify() { notifyObservers = false; }
632
638 void Notify() { notifyObservers = true; }
639
647 void NotifyObservers(const Types::qubits_vector &affectedQubits) {
648 if (!notifyObservers) return;
649
650 for (auto &observer : observers) {
651 observer->Update(affectedQubits);
652 }
653 }
654
655 private:
656 std::unordered_set<std::shared_ptr<ISimulatorObserver>>
657 observers;
658 bool notifyObservers =
659 true;
660};
661
662} // namespace Simulators
663
664#endif // !_SIMULATOR_STATE_H_
The operation interface.
Definition Operations.h:358
Interface class for a quantum computing simulator state.
Definition State.h:103
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:638
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:193
virtual void SetUpcomingGates(const std::vector< std::shared_ptr< Circuits::IOperation< double > > > &)
Supplies upcoming gates for lookahead swap optimization.
Definition State.h:234
void UnregisterObserver(const std::shared_ptr< ISimulatorObserver > &observer)
Unregisters an observer.
Definition State.h:471
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:631
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.
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:244
void RegisterObserver(const std::shared_ptr< ISimulatorObserver > &observer)
Registers an observer.
Definition State.h:459
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:253
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:204
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:480
void NotifyObservers(const Types::qubits_vector &affectedQubits)
Notifies observers.
Definition State.h:647
virtual void SetLookaheadDepth(int)
Sets the lookahead depth for swap optimization.
Definition State.h:214
virtual bool GetMultithreading() const =0
Get the multithreading flag.
virtual void SetLookaheadDepthWithHeuristic(int)
Sets the lookahead depth for swap optimization.
Definition State.h:225
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:261
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:183
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:85
@ 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:68
@ kCompositeQCSim
composite qcsim simulator type
@ kQCSim
qcsim simulator type
@ kQiskitAer
qiskit aer simulator type
@ kQuestSim
quest simulator type
@ kCompositeQiskitAer
composite qiskit aer simulator type
@ kGpuSim
gpu simulator type
uint_fast64_t qubit_t
The type of a qubit.
Definition Types.h:21
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.
Definition Types.h:23