Maestro 0.2.5
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
Reset.h
Go to the documentation of this file.
1
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 }
72 }
73
81 OperationType GetType() const override { return OperationType::kReset; }
82
92 const std::vector<bool> &resetTgts = {}) {
93 qubits = qs;
94 resetTargets = resetTgts;
95 }
96
103 const Types::qubits_vector &GetQubits() const { return qubits; }
104
112 const std::vector<bool> &GetResetTargets() const { return resetTargets; }
113
120 std::shared_ptr<IOperation<Time>> Clone() const override {
121 return std::make_shared<Reset<Time>>(
123 }
124
132 Types::qubits_vector AffectedQubits() const override { return qubits; }
133
144 std::shared_ptr<IOperation<Time>> Remap(
145 const std::unordered_map<Types::qubit_t, Types::qubit_t> &qubitsMap,
146 const std::unordered_map<Types::qubit_t, Types::qubit_t> &bitsMap = {})
147 const override {
148 auto newOp = this->Clone();
149
150 for (size_t i = 0; i < qubits.size(); ++i) {
151 const auto qubitit = qubitsMap.find(qubits[i]);
152 if (qubitit != qubitsMap.end())
153 std::static_pointer_cast<Reset<Time>>(newOp)->SetQubit(i,
154 qubitit->second);
155 }
156
157 return newOp;
158 }
159
169 bool IsClifford() const override { return true; }
170
171 protected:
179 void SetQubit(size_t index, Types::qubit_t qubit) {
180 if (index < qubits.size()) qubits[index] = qubit;
181 }
182
183 private:
184 Types::qubits_vector qubits;
185 std::vector<bool> resetTargets;
186 mutable XGate<Time> xgate;
188};
189
190} // namespace Circuits
191
192#endif // !_RESET_STATE_H_
The operation interface.
Definition Operations.h:358
Time GetDelay() const
Get the delay of the operation.
Definition Operations.h:497
The state class that stores the classical state of a quantum circuit execution.
Definition Operations.h:63
Reset operation class.
Definition Reset.h:33
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:91
std::shared_ptr< IOperation< Time > > Clone() const override
Get a shared pointer to a clone of this object.
Definition Reset.h:120
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:179
OperationType GetType() const override
Get the type of operation.
Definition Reset.h:81
const Types::qubits_vector & GetQubits() const
Get the qubits to reset.
Definition Reset.h:103
bool IsClifford() const override
Checks if the operation is a Clifford one.
Definition Reset.h:169
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:144
const std::vector< bool > & GetResetTargets() const
Get the values to reset the qubits to.
Definition Reset.h:112
Types::qubits_vector AffectedQubits() const override
Get the qubits affected by this operation.
Definition Reset.h:132
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 ...
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