Maestro 0.2.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
32template <typename Time = Types::time_type>
33class Reset : public IOperation<Time> {
34 public:
44 Reset(const Types::qubits_vector &qubits = {}, Time delay = 0,
45 const std::vector<bool> &resetTargets = {})
46 : IOperation<Time>(delay), qubits(qubits), resetTargets(resetTargets) {}
47
58 void Execute(const std::shared_ptr<Simulators::ISimulator> &sim,
59 OperationState &state) const override {
60 // Use the simulator's native ApplyReset which correctly handles
61 // per-backend measurement+flip (critical for MPS re-canonicalization).
62 sim->ApplyReset(qubits);
63
64 // If any reset targets specify |1⟩ instead of the default |0⟩,
65 // apply X to flip those qubits.
66 for (size_t qi = 0; qi < resetTargets.size() && qi < qubits.size(); ++qi) {
67 if (resetTargets[qi]) {
68 xgate.SetQubit(qubits[qi]);
69 xgate.Execute(sim, state);
70
71 if (sim->SupportsMPSSwapOptimization()) {
72 const auto counter = sim->GetGatesCounter();
73 if (counter > 0)
74 sim->SetGatesCounter(counter - 1);
75 }
76 }
77 }
78 }
79
87 OperationType GetType() const override { return OperationType::kReset; }
88
98 const std::vector<bool> &resetTgts = {}) {
99 qubits = qs;
100 resetTargets = resetTgts;
101 }
102
109 const Types::qubits_vector &GetQubits() const { return qubits; }
110
118 const std::vector<bool> &GetResetTargets() const { return resetTargets; }
119
126 std::shared_ptr<IOperation<Time>> Clone() const override {
127 return std::make_shared<Reset<Time>>(
129 }
130
138 Types::qubits_vector AffectedQubits() const override { return qubits; }
139
150 std::shared_ptr<IOperation<Time>> Remap(
151 const std::unordered_map<Types::qubit_t, Types::qubit_t> &qubitsMap,
152 const std::unordered_map<Types::qubit_t, Types::qubit_t> &bitsMap = {})
153 const override {
154 auto newOp = this->Clone();
155
156 for (size_t i = 0; i < qubits.size(); ++i) {
157 const auto qubitit = qubitsMap.find(qubits[i]);
158 if (qubitit != qubitsMap.end())
159 std::static_pointer_cast<Reset<Time>>(newOp)->SetQubit(i,
160 qubitit->second);
161 }
162
163 return newOp;
164 }
165
175 bool IsClifford() const override { return true; }
176
177 protected:
185 void SetQubit(size_t index, Types::qubit_t qubit) {
186 if (index < qubits.size()) qubits[index] = qubit;
187 }
188
189 private:
190 Types::qubits_vector qubits;
191 std::vector<bool> resetTargets;
192 mutable XGate<Time> xgate;
194};
195
196} // namespace Circuits
197
198#endif // !_RESET_STATE_H_
Time GetDelay() const
Get the delay of the operation.
Definition Operations.h:497
IOperation(Types::time_type delay=0)
Definition Operations.h:366
The state class that stores the classical state of a quantum circuit execution.
Definition Operations.h:63
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:97
std::shared_ptr< IOperation< Time > > Clone() const override
Get a shared pointer to a clone of this object.
Definition Reset.h:126
Reset(const Types::qubits_vector &qubits={}, Time delay=0, const std::vector< bool > &resetTargets={})
Construct a new Reset object.
Definition Reset.h:44
void Execute(const std::shared_ptr< Simulators::ISimulator > &sim, OperationState &state) const override
Execute the operation.
Definition Reset.h:58
void SetQubit(size_t index, Types::qubit_t qubit)
Set the qubit to reset.
Definition Reset.h:185
OperationType GetType() const override
Get the type of operation.
Definition Reset.h:87
const Types::qubits_vector & GetQubits() const
Get the qubits to reset.
Definition Reset.h:109
bool IsClifford() const override
Checks if the operation is a Clifford one.
Definition Reset.h:175
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:150
const std::vector< bool > & GetResetTargets() const
Get the values to reset the qubits to.
Definition Reset.h:118
Types::qubits_vector AffectedQubits() const override
Get the qubits affected by this operation.
Definition Reset.h:138
The X gate.
OperationType
The type of operations.
Definition Operations.h:27
@ kReset
reset, no result in 'state', just apply measurement, then apply not on all qubits that were measured ...
Definition Operations.h:39
std::vector< qubit_t > qubits_vector
The type of a vector of qubits.
Definition Types.h:22
uint_fast64_t qubit_t
The type of a qubit.
Definition Types.h:21