2#if defined(INCLUDED_BY_FACTORY)
13 using RoutingBase = TensorChainRouting<QCSimGateSimulator>;
16 bool IsRoutingLookaheadEnabled()
const override
18 return RoutingBase::IsRoutingLookaheadEnabled() || (UsesBoundedRouting() && CurrentRoutingMap().size() == nrQubits);
21 void SetLookaheadDepth(
int depth)
override
23 automaticRoutingDepth =
false;
24 InvalidateBoundedRouting();
25 RoutingBase::SetLookaheadDepth(depth);
28 void SetAutomaticLookaheadDepth(
int depth)
override
30 automaticRoutingDepth =
true;
31 InvalidateBoundedRouting();
32 RoutingBase::SetLookaheadDepth(depth);
35 void SetUpcomingGates(
const std::vector<std::shared_ptr<Circuits::IOperation<double>>> &gates)
override
37 InvalidateBoundedRouting(
true);
38 RoutingBase::SetUpcomingGates(gates);
41 void setGrowthFactorSwap(
double factor)
override
43 RoutingBase::setGrowthFactorSwap(factor);
44 RefreshRoutingCallback();
47 void setGrowthFactorGate(
double factor)
override
49 RoutingBase::setGrowthFactorGate(factor);
50 RefreshRoutingCallback();
55 if (key && std::string(key) == RoutingConfig::kStrategy && !configuration.IsSet(key))
57 return RoutingBase::GetConfiguration(key);
61 explicit QCSimTensorChainSimulator(
bool isOperatorChain) : TensorChainRouting(isOperatorChain, true)
63 meetingPositionCallback = [
this](
const auto &bonds) -> QC::TensorNetworks::MPSSimulatorInterface::IndexType {
66 if (!decompositionDepth && upcomingGateIndex >= 0)
68 if (!boundedRoutingPrepared && UsesBoundedRouting() &&
static_cast<size_t>(upcomingGateIndex) < upcomingGates.size())
70 const auto &operation = upcomingGates[
static_cast<size_t>(upcomingGateIndex)];
74 PrepareBoundedRouting();
77 return boundedRouting->Select(CurrentRoutingMap(), bonds.data(), bonds.size(),
static_cast<size_t>(upcomingGateIndex));
79 return PlanMeetingPosition(bonds.data(), bonds.size());
83 InvalidateDummyHistory();
87 bondDimensionCallback = [
this](
auto maximum) { curMaxBondDim = std::max(curMaxBondDim,
static_cast<size_t>(maximum)); };
90 bool UsesBoundedRouting()
const
94 return automaticRoutingDepth && useOptimalMeetingPosition && lookaheadDepth == 0 && nrQubits > 1 &&
95 RoutingConfig::GetStrategy(configuration) == RoutingConfig::Strategy::Combined && !virtualBondsEnabled && growthFactorGate == 0.7 &&
96 growthFactorSwap == 1. && (operatorChain ? nrQubits <= 8 : nrQubits > 10);
99 void InvalidateBoundedRouting(
bool operationsChanged =
false)
101 boundedRouting.reset();
102 boundedRoutingPrepared =
false;
103 if (operationsChanged)
105 boundedRoutingOperations.reset();
106 boundedRoutingOperationsQubits = 0;
112 void RefreshBoundedRouting()
114 if (boundedRoutingOperations && boundedRoutingOperationsQubits != nrQubits)
115 InvalidateBoundedRouting(
true);
116 else if (!UsesBoundedRouting() || upcomingGates.empty() ||
117 (boundedRoutingPrepared && (boundedRoutingQubits != nrQubits || boundedRoutingCap != physicalBondCap)))
118 InvalidateBoundedRouting();
121 void PrepareBoundedRouting()
123 if (!UsesBoundedRouting() || upcomingGates.empty())
125 InvalidateBoundedRouting();
128 if (boundedRoutingPrepared && boundedRoutingQubits == nrQubits && boundedRoutingCap == physicalBondCap)
130 boundedRouting.reset();
131 boundedRoutingPrepared =
true;
132 boundedRoutingQubits = nrQubits;
133 boundedRoutingCap = physicalBondCap;
136 if (!boundedRoutingOperations || boundedRoutingOperationsQubits != nrQubits)
138 boundedRoutingOperations = BoundedRoutingPlanner::PrepareOperations(nrQubits, upcomingGates);
139 boundedRoutingOperationsQubits = nrQubits;
142 std::make_unique<BoundedRoutingPlanner>(nrQubits, operatorChain, physicalBondCap, boundedRoutingOperations,
143 operatorChain ? BoundedRoutingPlanner::Policy::Work4 : BoundedRoutingPlanner::Policy::Work24);
152 bool automaticRoutingDepth =
false;
153 std::unique_ptr<BoundedRoutingPlanner> boundedRouting;
154 BoundedRoutingPlanner::SharedOperations boundedRoutingOperations;
155 size_t boundedRoutingOperationsQubits = 0;
156 bool boundedRoutingPrepared =
false;
157 size_t boundedRoutingQubits = 0;
158 MPSDummySimulator::IndexType boundedRoutingCap = 0;
160 QC::TensorNetworks::MPSSimulator::MeetingPositionCallback meetingPositionCallback =
nullptr;
161 std::function<void(Eigen::Index)> bondDimensionCallback =
nullptr;
char * GetConfiguration(void *sim, const char *key)
@ kGate
the usual quantum gate, result stays in simulator's state