Maestro 0.3.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
Simulator.h
Go to the documentation of this file.
1
13
14#pragma once
15
16#ifndef _SIMULATOR_INTERFACE_H_
17#define _SIMULATOR_INTERFACE_H_
18
19#include "State.h"
20
21namespace Simulators
22{
24{
25 uint64_t submittedGates = 0;
26 uint64_t backendGates = 0;
27 uint64_t fusedBlocks = 0;
28};
29
40class ISimulator : public IState, std::enable_shared_from_this<ISimulator>
41{
42 public:
43 using Matrix8cd = Eigen::Matrix<std::complex<double>, 8, 8>;
44
45 // Opt in to Maestro fusion; immediate implementations keep the default.
46 virtual unsigned GetGateFusionMaxQubits() const
47 {
48 return 0;
49 }
50
51 virtual bool IsGateFusionEnabled() const
52 {
53 return false;
54 }
55
57 {
58 return {};
59 }
60
61 // Emits gates held back for fusion without waiting for the backend to
62 // execute them. Flush() also waits; reads of the state flush on their own.
63 virtual void FlushPendingGates()
64 {
65 }
66
68 {
69 throw std::runtime_error("Generic three-qubit gates are unsupported");
70 }
71
72 // Native CUDA device, or -1 for CPU/uninitialized simulators.
73 virtual int GetGpuDevice() const
74 {
75 return -1;
76 }
77
83 virtual void ApplyGenericOneQubitGate(Types::qubit_t qubit, const Eigen::Matrix2cd &gate) = 0;
84
91 virtual void ApplyGenericTwoQubitGate(Types::qubit_t qubit0, Types::qubit_t qubit1, const Eigen::Matrix4cd &gate) = 0;
92
100 virtual void ApplyP(Types::qubit_t qubit, double lambda) = 0;
101
108 virtual void ApplyX(Types::qubit_t qubit) = 0;
109
116 virtual void ApplyY(Types::qubit_t qubit) = 0;
117
124 virtual void ApplyZ(Types::qubit_t qubit) = 0;
125
132 virtual void ApplyH(Types::qubit_t qubit) = 0;
133
140 virtual void ApplyS(Types::qubit_t qubit) = 0;
141
148 virtual void ApplySDG(Types::qubit_t qubit) = 0;
149
156 virtual void ApplyT(Types::qubit_t qubit) = 0;
157
164 virtual void ApplyTDG(Types::qubit_t qubit) = 0;
165
172 virtual void ApplySx(Types::qubit_t qubit) = 0;
173
180 virtual void ApplySxDAG(Types::qubit_t qubit) = 0;
181
188 virtual void ApplyK(Types::qubit_t qubit) = 0;
189
197 virtual void ApplyRx(Types::qubit_t qubit, double theta) = 0;
198
206 virtual void ApplyRy(Types::qubit_t qubit, double theta) = 0;
207
215 virtual void ApplyRz(Types::qubit_t qubit, double theta) = 0;
216
227 virtual void ApplyU(Types::qubit_t qubit, double theta, double phi, double lambda, double gamma) = 0;
228
236 virtual void ApplyCX(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit) = 0;
237
245 virtual void ApplyCY(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit) = 0;
246
254 virtual void ApplyCZ(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit) = 0;
255
264 virtual void ApplyCP(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double lambda) = 0;
265
274 virtual void ApplyCRx(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double theta) = 0;
275
284 virtual void ApplyCRy(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double theta) = 0;
285
294 virtual void ApplyCRz(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double theta) = 0;
295
303 virtual void ApplyCH(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit) = 0;
304
312 virtual void ApplyCSx(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit) = 0;
313
321 virtual void ApplyCSxDAG(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit) = 0;
322
330 virtual void ApplySwap(Types::qubit_t qubit0, Types::qubit_t qubit1) = 0;
331
340 virtual void ApplyCCX(Types::qubit_t qubit0, Types::qubit_t qubit1, Types::qubit_t qubit2) = 0;
341
350 virtual void ApplyCSwap(Types::qubit_t ctrl_qubit, Types::qubit_t qubit0, Types::qubit_t qubit1) = 0;
351
363 virtual void ApplyCU(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double theta, double phi, double lambda, double gamma) = 0;
364
372 virtual void ApplyNop() = 0;
373
382 void InitializeToBasisState(size_t num_qubits, Types::qubit_t basisState) override
383 {
384 if (num_qubits == 0)
385 return;
386 Clear();
387 AllocateQubits(num_qubits);
388 Initialize();
389 for (size_t q = 0; q < num_qubits; ++q)
390 if ((basisState >> q) & 1ULL)
391 ApplyX(static_cast<Types::qubit_t>(q));
392 }
393
400 void InitializeToBasisState(size_t num_qubits, const std::vector<bool> &basisState) override
401 {
402 if (num_qubits == 0)
403 return;
404 Clear();
405 AllocateQubits(num_qubits);
406 Initialize();
407 for (size_t q = 0; q < num_qubits && q < basisState.size(); ++q)
408 if (basisState[q])
409 ApplyX(static_cast<Types::qubit_t>(q));
410 }
411
422 virtual std::unique_ptr<ISimulator> Clone() = 0;
423
424 // A logical execution stream is assigned before jobs enter the worker pool.
425 // Backends can copy state without consuming their ordinary clone ordinal.
426 virtual std::unique_ptr<ISimulator> CloneForExecution(uint64_t seed)
427 {
428 auto clone = Clone();
429 clone->SetSeed(seed);
430 return clone;
431 }
432
440 std::shared_ptr<ISimulator> getptr()
441 {
442 return shared_from_this();
443 }
444};
445} // namespace Simulators
446
447#endif // !_SIMULATOR_INTERFACE_H_
Interface class for a quantum computing simulator.
Definition Simulator.h:41
virtual std::unique_ptr< ISimulator > CloneForExecution(uint64_t seed)
Definition Simulator.h:426
virtual void ApplyCRz(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double theta)=0
Applies a CRz gate to the qubits.
virtual void ApplySDG(Types::qubit_t qubit)=0
Applies a S dagger gate to the qubit.
virtual void ApplyCCX(Types::qubit_t qubit0, Types::qubit_t qubit1, Types::qubit_t qubit2)=0
Applies a controlled controlled not gate to the qubits.
virtual bool IsGateFusionEnabled() const
Definition Simulator.h:51
virtual void ApplyX(Types::qubit_t qubit)=0
Applies a not gate to the qubit.
virtual void ApplyCSxDAG(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit)=0
Applies a CSx dagger gate to the qubits.
std::shared_ptr< ISimulator > getptr()
Get a shared pointer to this object.
Definition Simulator.h:440
virtual void ApplyU(Types::qubit_t qubit, double theta, double phi, double lambda, double gamma)=0
Applies a U gate to the qubit.
virtual void ApplyP(Types::qubit_t qubit, double lambda)=0
Applies a phase shift gate to the qubit.
virtual void ApplySx(Types::qubit_t qubit)=0
Applies a Sx gate to the qubit.
void InitializeToBasisState(size_t num_qubits, Types::qubit_t basisState) override
Initializes the state to a computational basis state.
Definition Simulator.h:382
virtual void FlushPendingGates()
Definition Simulator.h:63
virtual void ApplyTDG(Types::qubit_t qubit)=0
Applies a T dagger gate to the qubit.
virtual void ApplyCU(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double theta, double phi, double lambda, double gamma)=0
Applies a controlled U gate to the qubits.
virtual void ApplyGenericThreeQubitGate(Types::qubit_t, Types::qubit_t, Types::qubit_t, const Matrix8cd &)
Definition Simulator.h:67
virtual void ApplyCRy(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double theta)=0
Applies a CRy gate to the qubits.
virtual void ApplyCX(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit)=0
Applies a CX gate to the qubits.
virtual void ApplyRy(Types::qubit_t qubit, double theta)=0
Applies a Ry gate to the qubit.
Eigen::Matrix< std::complex< double >, 8, 8 > Matrix8cd
Definition Simulator.h:43
void InitializeToBasisState(size_t num_qubits, const std::vector< bool > &basisState) override
Initializes the state to a computational basis state.
Definition Simulator.h:400
virtual void ApplyCH(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit)=0
Applies a CH gate to the qubits.
virtual void ApplySwap(Types::qubit_t qubit0, Types::qubit_t qubit1)=0
Applies a swap gate to the qubits.
virtual void ApplyCSx(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit)=0
Applies a CSx gate to the qubits.
virtual void ApplyGenericTwoQubitGate(Types::qubit_t qubit0, Types::qubit_t qubit1, const Eigen::Matrix4cd &gate)=0
Apply a generic two-qubit gate to the specified qubits.
virtual void ApplyK(Types::qubit_t qubit)=0
Applies a K gate to the qubit.
virtual void ApplyY(Types::qubit_t qubit)=0
Applies a Y gate to the qubit.
virtual GateFusionStatistics GetGateFusionStatistics() const
Definition Simulator.h:56
virtual void ApplyT(Types::qubit_t qubit)=0
Applies a T gate to the qubit.
virtual void ApplyS(Types::qubit_t qubit)=0
Applies a S gate to the qubit.
virtual int GetGpuDevice() const
Definition Simulator.h:73
virtual void ApplyNop()=0
Applies a nop.
virtual void ApplyCSwap(Types::qubit_t ctrl_qubit, Types::qubit_t qubit0, Types::qubit_t qubit1)=0
Applies a controlled swap gate to the qubits.
virtual void ApplyZ(Types::qubit_t qubit)=0
Applies a Z gate to the qubit.
virtual void ApplyRz(Types::qubit_t qubit, double theta)=0
Applies a Rz gate to the qubit.
virtual void ApplyCRx(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double theta)=0
Applies a CRx gate to the qubits.
virtual void ApplyCY(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit)=0
Applies a CY gate to the qubits.
virtual void ApplyCZ(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit)=0
Applies a CZ gate to the qubits.
virtual std::unique_ptr< ISimulator > Clone()=0
Clones the simulator.
virtual void ApplyH(Types::qubit_t qubit)=0
Applies a Hadamard gate to the qubit.
virtual void ApplyRx(Types::qubit_t qubit, double theta)=0
Applies a Rx gate to the qubit.
virtual unsigned GetGateFusionMaxQubits() const
Definition Simulator.h:46
virtual void ApplySxDAG(Types::qubit_t qubit)=0
Applies a Sx dagger gate to the qubit.
virtual void ApplyCP(Types::qubit_t ctrl_qubit, Types::qubit_t tgt_qubit, double lambda)=0
Applies a CP gate to the qubits.
virtual void ApplyGenericOneQubitGate(Types::qubit_t qubit, const Eigen::Matrix2cd &gate)=0
Apply a generic one-qubit gate to the specified qubit.
Interface class for a quantum computing simulator state.
Definition State.h:131
virtual void Initialize()=0
Initializes the state.
virtual size_t AllocateQubits(size_t num_qubits)=0
Allocates qubits.
virtual void Clear()=0
Clears the state.
Bounded online routing based on current physical bond dimensions.
uint_fast64_t qubit_t
The type of a qubit.
Definition Types.h:23