3232#include " dd/StateGeneration.hpp"
3333#include " ir/Definitions.hpp"
3434#include " ir/Register.hpp"
35- #include " ir/operations/ClassicControlledOperation .hpp"
35+ #include " ir/operations/IfElseOperation .hpp"
3636#include " ir/operations/OpType.hpp"
3737#include " qasm3/Importer.hpp"
3838
@@ -838,13 +838,21 @@ Result ddsimStepForward(SimulationState* self) {
838838 ddsim->iterator ++;
839839 return OK;
840840 }
841- if ((*ddsim->iterator )->isClassicControlledOperation ()) {
842- // For classic-controlled operations, we need to read the values of the
843- // classical register first.
841+ if ((*ddsim->iterator )->isIfElseOperation ()) {
842+ // For if-else operations, we need to read the values of the classical
843+ // register first.
844844 const auto * op =
845- dynamic_cast <qc::ClassicControlledOperation*>((*ddsim->iterator ).get ());
845+ dynamic_cast <qc::IfElseOperation*>((*ddsim->iterator ).get ());
846+ if (op->getComparisonKind () != qc::Eq) {
847+ throw std::runtime_error (" If-else operations with non-equality "
848+ " comparisons are currently not supported" );
849+ }
850+ if (op->getControlBit ().has_value ()) {
851+ throw std::runtime_error (" If-else operations controlled by a single "
852+ " classical bit are currently not supported" );
853+ }
846854 const auto & controls = op->getControlRegister ();
847- const auto & exp = op->getExpectedValue ();
855+ const auto & exp = op->getExpectedValueRegister ();
848856 size_t registerValue = 0 ;
849857 for (size_t i = 0 ; i < controls->getSize (); i++) {
850858 const auto name =
@@ -853,7 +861,11 @@ Result ddsimStepForward(SimulationState* self) {
853861 registerValue |= (value ? 1ULL : 0ULL ) << i;
854862 }
855863 if (registerValue == exp) {
856- currDD = dd::getDD (**ddsim->iterator , *ddsim->dd );
864+ auto * thenOp = op->getThenOp ();
865+ currDD = dd::getDD (*thenOp, *ddsim->dd );
866+ } else if (op->getElseOp () != nullptr ) {
867+ auto * elseOp = op->getElseOp ();
868+ currDD = dd::getDD (*elseOp, *ddsim->dd );
857869 } else {
858870 currDD = dd::Package::makeIdent ();
859871 }
@@ -915,11 +927,19 @@ Result ddsimStepBackward(SimulationState* self) {
915927 if ((*ddsim->iterator )->getType () == qc::Barrier) {
916928 return OK;
917929 }
918- if ((*ddsim->iterator )->isClassicControlledOperation ()) {
930+ if ((*ddsim->iterator )->isIfElseOperation ()) {
919931 const auto * op =
920- dynamic_cast <qc::ClassicControlledOperation*>((*ddsim->iterator ).get ());
932+ dynamic_cast <qc::IfElseOperation*>((*ddsim->iterator ).get ());
933+ if (op->getComparisonKind () != qc::Eq) {
934+ throw std::runtime_error (" If-else operations with non-equality "
935+ " comparisons are currently not supported" );
936+ }
937+ if (op->getControlBit ().has_value ()) {
938+ throw std::runtime_error (" If-else operations controlled by a single "
939+ " classical bit are currently not supported" );
940+ }
921941 const auto & controls = op->getControlRegister ();
922- const auto & exp = op->getExpectedValue ();
942+ const auto & exp = op->getExpectedValueRegister ();
923943 size_t registerValue = 0 ;
924944 for (size_t i = 0 ; i < controls->getSize (); i++) {
925945 const auto name =
@@ -928,7 +948,11 @@ Result ddsimStepBackward(SimulationState* self) {
928948 registerValue |= (value ? 1ULL : 0ULL ) << i;
929949 }
930950 if (registerValue == exp) {
931- currDD = dd::getInverseDD (**ddsim->iterator , *ddsim->dd );
951+ auto * thenOp = op->getThenOp ();
952+ currDD = dd::getInverseDD (*thenOp, *ddsim->dd );
953+ } else if (op->getElseOp () != nullptr ) {
954+ auto * elseOp = op->getElseOp ();
955+ currDD = dd::getInverseDD (*elseOp, *ddsim->dd );
932956 } else {
933957 currDD = dd::Package::makeIdent ();
934958 }
0 commit comments