18#ifndef _CONDITIONAL_H_
19#define _CONDITIONAL_H_
35class ICondition :
public std::enable_shared_from_this<ICondition> {
43 ICondition(
const std::vector<size_t> &ind) : indices(ind) {}
86 virtual std::shared_ptr<ICondition>
Clone()
const = 0;
97 virtual std::shared_ptr<ICondition>
Remap(
98 const std::unordered_map<Types::qubit_t, Types::qubit_t> &bitsMap = {})
109 return std::enable_shared_from_this<ICondition>::shared_from_this();
164 void SetBits(
const std::vector<bool> &b) { bits = b; }
172 std::shared_ptr<ICondition>
Clone()
const override {
186 const std::unordered_map<Types::qubit_t, Types::qubit_t> &bitsMap = {})
188 auto newCond = this->
Clone();
190 auto newBits = newCond->GetBitsIndices();
191 for (
size_t i = 0; i < newBits.size(); ++i) {
192 const auto bitit = bitsMap.find(newBits[i]);
193 if (bitit != bitsMap.end()) newBits[i] = bitit->second;
197 newCond->SetBitsIndices(newBits);
203 std::vector<bool> bits;
216template <
typename Time = Types::time_type>
231 const std::shared_ptr<ICondition> &condition,
233 :
IOperation<Time>(delay), operation(operation), condition(condition) {}
247 void Execute(
const std::shared_ptr<Simulators::ISimulator> &sim,
249 if (!condition || !operation)
return;
251 if (condition->IsConditionMet(state)) operation->Execute(sim, state);
283 std::shared_ptr<IOperation<Time>>
GetOperation()
const {
return operation; }
312 if (!condition)
return {};
314 return condition->GetBitsIndices();
324 if (!operation)
return {};
326 return operation->AffectedQubits();
339 std::shared_ptr<IOperation<Time>>
Remap(
340 const std::unordered_map<Types::qubit_t, Types::qubit_t> &qubitsMap,
341 const std::unordered_map<Types::qubit_t, Types::qubit_t> &bitsMap = {})
344 std::static_pointer_cast<IConditionalOperation<Time>>(this->
Clone());
346 condOp->SetCondition(condOp->GetCondition()->Remap(bitsMap));
347 condOp->SetOperation(condOp->GetOperation()->Remap(qubitsMap, bitsMap));
362 if (!operation)
return true;
364 return operation->IsClifford();
368 std::shared_ptr<IOperation<Time>>
370 std::shared_ptr<ICondition> condition;
382template <
typename Time = Types::time_type>
398 const std::shared_ptr<ICondition> &condition, Time delay = 0)
418 std::shared_ptr<IOperation<Time>>
Clone()
const override {
419 const auto gate = std::static_pointer_cast<IGateOperation<Time>>(
421 const auto cond = std::static_pointer_cast<ICondition>(
424 return std::make_shared<ConditionalGate<Time>>(
438template <
typename Time = Types::time_type>
455 const std::shared_ptr<ICondition> &condition, Time delay = 0)
475 std::shared_ptr<IOperation<Time>>
Clone()
const override {
476 return std::make_shared<ConditionalMeasurement<Time>>(
477 std::static_pointer_cast<MeasurementOperation<Time>>(
479 std::static_pointer_cast<ICondition>(
494template <
typename Time = Types::time_type>
510 const std::shared_ptr<ICondition> &condition,
531 std::shared_ptr<IOperation<Time>>
Clone()
const override {
532 return std::make_shared<ConditionalRandomGen<Time>>(
533 std::static_pointer_cast<Random<Time>>(
535 std::static_pointer_cast<ICondition>(
A conditional quantum gate.
ConditionalGate(const std::shared_ptr< IGateOperation< Time > > &operation, const std::shared_ptr< ICondition > &condition, Time delay=0)
Construct a new ConditionalGate object.
OperationType GetType() const override
Get the type of the operation.
std::shared_ptr< IOperation< Time > > Clone() const override
Get a shared pointer to a clone of this object.
A conditional measurement.
ConditionalMeasurement(const std::shared_ptr< MeasurementOperation< Time > > &operation, const std::shared_ptr< ICondition > &condition, Time delay=0)
Construct a new ConditionalMeasurement object.
OperationType GetType() const override
Get the type of the operation.
std::shared_ptr< IOperation< Time > > Clone() const override
Get a shared pointer to a clone of this object.
A conditional random generator.
OperationType GetType() const override
Get the type of the operation.
ConditionalRandomGen(const std::shared_ptr< Random< Time > > &operation, const std::shared_ptr< ICondition > &condition, Time delay=0)
Construct a new ConditionalRandomGen object.
Types::qubits_vector AffectedQubits() const override
Get the quantum bits affected by the operation.
std::shared_ptr< IOperation< Time > > Clone() const override
Get a shared pointer to a clone of this object.
Condition that checks if the bits are equal to a given value.
std::shared_ptr< ICondition > Remap(const std::unordered_map< Types::qubit_t, Types::qubit_t > &bitsMap={}) const override
Get a shared pointer to a remapped condition.
bool IsConditionMet(OperationState &state) const override
Check if the condition is met.
const std::vector< bool > & GetAllBits() const
Get the values to compare the bits to.
std::shared_ptr< ICondition > Clone() const override
Get a shared pointer to a clone of this object.
void SetBits(const std::vector< bool > &b)
Set the values to compare the bits to.
EqualCondition(const std::vector< size_t > &ind, const std::vector< bool > &b)
Construct a new EqualCondition object.
Interface for a condition.
virtual ~ICondition()=default
Virtual destructor.
virtual bool IsConditionMet(OperationState &state) const =0
Check if the condition is met.
const std::vector< size_t > & GetBitsIndices() const
Get the indices of the bits used in the condition.
std::shared_ptr< ICondition > getptr()
Get a shared pointer to this object.
void SetBitsIndices(const std::vector< size_t > &ind)
Set the indices of the bits used in the condition.
virtual std::shared_ptr< ICondition > Remap(const std::unordered_map< Types::qubit_t, Types::qubit_t > &bitsMap={}) const =0
Get a shared pointer to a remapped condition.
virtual std::shared_ptr< ICondition > Clone() const =0
Get a shared pointer to a clone of this object.
ICondition(const std::vector< size_t > &ind)
Construct a new ICondition object.
An operation conditioned on classical values.
void SetCondition(const std::shared_ptr< ICondition > &cond)
Set the condition for the conditional operation.
bool IsClifford() const override
Checks if the operation is a Clifford one.
std::shared_ptr< IOperation< Time > > GetOperation() const
Get the operation for the conditional operation.
IConditionalOperation(const std::shared_ptr< IOperation< Time > > &operation, const std::shared_ptr< ICondition > &condition, Time delay=0)
Construct a new IConditionalOperation object.
void SetOperation(const std::shared_ptr< IOperation< Time > > &op)
Set the operation for the conditional operation.
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.
std::vector< size_t > AffectedBits() const override
Get bits that are involved in the condition.
std::shared_ptr< ICondition > GetCondition() const
Get the condition for the conditional operation.
void Execute(const std::shared_ptr< Simulators::ISimulator > &sim, OperationState &state) const override
Execute the operation.
Types::qubits_vector AffectedQubits() const override
Get the qubits affected by the operation.
The gate operation interface.
Time GetDelay() const
Get the delay of the operation.
virtual std::shared_ptr< IOperation< Time > > Clone() const =0
Get a shared pointer to a clone of this object.
Measurement operation class.
The state class that stores the classical state of a quantum circuit execution.
std::vector< bool > GetBits(const std::vector< size_t > &indices) const
Get the classical bits at the specified indices.
OperationType
The type of operations.
@ kConditionalGate
conditional gate, similar with gate, but conditioned on something from 'OperationState'
@ kRandomGen
random classical bit generator, result in 'OperationState'
@ kConditionalRandomGen
conditional random generator, similar with random gen, but conditioned on something from 'OperationSt...
@ kConditionalMeasurement
conditional measurement, similar with measurement, but conditioned on something from 'OperationState'
@ kMeasurement
measurement, result in 'OperationState'
@ kGate
the usual quantum gate, result stays in simulator's state