Maestro 0.3.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
Reset.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#ifndef _RESET_STATE_H_
18#define _RESET_STATE_H_
19
20#include "QuantumGates.h"
21
22namespace Circuits
23{
24
33template <typename Time = Types::time_type> class Reset : public IOperation<Time>
34{
35 public:
45 Reset(const Types::qubits_vector &qubits = {}, Time delay = 0, const std::vector<bool> &resetTargets = {})
46 : IOperation<Time>(delay), qubits(qubits), resetTargets(resetTargets)
47 {
48 }
49
60 void Execute(const std::shared_ptr<Simulators::ISimulator> &sim, OperationState &state) const override
61 {
62 // Use the simulator's native ApplyReset which correctly handles
63 // per-backend measurement+flip (critical for MPS re-canonicalization).
64 sim->ApplyReset(qubits);
65
66 // If any reset targets specify |1⟩ instead of the default |0⟩,
67 // apply X to flip those qubits.
68 for (size_t qi = 0; qi < resetTargets.size() && qi < qubits.size(); ++qi)
69 {
70 if (resetTargets[qi])
71 {
72 // Jobs can share this instruction; only mutate their own simulator.
73 sim->ApplyX(qubits[qi]);
74
75 if (sim->SupportsMPSSwapOptimization())
76 {
77 const auto counter = sim->GetGatesCounter();
78 if (counter > 0)
79 sim->SetGatesCounter(counter - 1);
80 }
81 }
82 }
83 }
84
92 OperationType GetType() const override
93 {
95 }
96
105 void SetQubits(const Types::qubits_vector &qs, const std::vector<bool> &resetTgts = {})
106 {
107 qubits = qs;
108 resetTargets = resetTgts;
109 }
110
118 {
119 return qubits;
120 }
121
129 const std::vector<bool> &GetResetTargets() const
130 {
131 return resetTargets;
132 }
133
140 std::shared_ptr<IOperation<Time>> Clone() const override
141 {
142 return std::make_shared<Reset<Time>>(GetQubits(), IOperation<Time>::GetDelay(), GetResetTargets());
143 }
144
153 {
154 return qubits;
155 }
156
167 std::shared_ptr<IOperation<Time>> Remap(const std::unordered_map<Types::qubit_t, Types::qubit_t> &qubitsMap,
168 const std::unordered_map<Types::qubit_t, Types::qubit_t> &bitsMap = {}) const override
169 {
170 auto newOp = this->Clone();
171
172 for (size_t i = 0; i < qubits.size(); ++i)
173 {
174 const auto qubitit = qubitsMap.find(qubits[i]);
175 if (qubitit != qubitsMap.end())
176 std::static_pointer_cast<Reset<Time>>(newOp)->SetQubit(i, qubitit->second);
177 }
178
179 return newOp;
180 }
181
191 bool IsClifford() const override
192 {
193 return true;
194 }
195
196 protected:
204 void SetQubit(size_t index, Types::qubit_t qubit)
205 {
206 if (index < qubits.size())
207 qubits[index] = qubit;
208 }
209
210 private:
211 Types::qubits_vector qubits;
212 std::vector<bool> resetTargets;
213};
214
215} // namespace Circuits
216
217#endif // !_RESET_STATE_H_
Time GetDelay() const
Get the delay of the operation.
Definition Operations.h:546
IOperation(Types::time_type delay=0)
Definition Operations.h:410
The state class that stores the classical state of a quantum circuit execution.
Definition Operations.h:68
void SetQubits(const Types::qubits_vector &qs, const std::vector< bool > &resetTgts={})
Set the qubits to reset and the values to reset them to.
Definition Reset.h:105
std::shared_ptr< IOperation< Time > > Clone() const override
Get a shared pointer to a clone of this object.
Definition Reset.h:140
Reset(const Types::qubits_vector &qubits={}, Time delay=0, const std::vector< bool > &resetTargets={})
Construct a new Reset object.
Definition Reset.h:45
void Execute(const std::shared_ptr< Simulators::ISimulator > &sim, OperationState &state) const override
Execute the operation.
Definition Reset.h:60
void SetQubit(size_t index, Types::qubit_t qubit)
Set the qubit to reset.
Definition Reset.h:204
OperationType GetType() const override
Get the type of operation.
Definition Reset.h:92
const Types::qubits_vector & GetQubits() const
Get the qubits to reset.
Definition Reset.h:117
bool IsClifford() const override
Checks if the operation is a Clifford one.
Definition Reset.h:191
std::shared_ptr< IOperation< Time > > Remap(const std::unordered_map< Types::qubit_t, Types::qubit_t > &qubitsMap, const std::unordered_map< Types::qubit_t, Types::qubit_t > &bitsMap={}) const override
Get a shared pointer to a remapped operation.
Definition Reset.h:167
const std::vector< bool > & GetResetTargets() const
Get the values to reset the qubits to.
Definition Reset.h:129
Types::qubits_vector AffectedQubits() const override
Get the qubits affected by this operation.
Definition Reset.h:152
OperationType
The type of operations.
Definition Operations.h:29
@ kReset
reset, no result in 'state', just apply measurement, then apply not on all qubits that were measured ...
Definition Operations.h:41
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.
Definition Types.h:24
uint_fast64_t qubit_t
The type of a qubit.
Definition Types.h:23