2323
2424#include " ../Simulators/MPSDummySimulator.h"
2525
26-
2726namespace Network {
2827
2928/* *
@@ -407,7 +406,8 @@ class SimpleDisconnectedNetwork : public INetwork<Time> {
407406 const size_t n = simulator->GetNumberOfQubits ();
408407 const size_t dim = 1ULL << n;
409408 amplitudes.resize (dim);
410- for (size_t state = 0 ; state < dim; ++state) amplitudes[state] = simulator->Amplitude (state);
409+ for (size_t state = 0 ; state < dim; ++state)
410+ amplitudes[state] = simulator->Amplitude (state);
411411
412412 // Remap amplitudes back to the original qubit ordering if qubits were
413413 // remapped during execution on the host.
@@ -419,8 +419,7 @@ class SimpleDisconnectedNetwork : public INetwork<Time> {
419419 std::vector<size_t > simToOrig (n);
420420 size_t offset = offsetBase;
421421
422- for (size_t qbit = 0 ; qbit < n; ++qbit)
423- {
422+ for (size_t qbit = 0 ; qbit < n; ++qbit) {
424423 auto pos = qubitsMapOnHost.find (qbit);
425424 if (pos != qubitsMapOnHost.end ())
426425 simToOrig[pos->second ] = pos->first ;
@@ -652,7 +651,7 @@ class SimpleDisconnectedNetwork : public INetwork<Time> {
652651 if (optSim) {
653652 job->optSim = optSim->Clone ();
654653 job->executedGates = executed;
655- }
654+ }
656655
657656 threadsPool.AddRunJob (std::move (job));
658657 }
@@ -680,7 +679,7 @@ class SimpleDisconnectedNetwork : public INetwork<Time> {
680679 } else {
681680 if (simulator && method == saveMethod && simType == saveSimType) {
682681 // use the already created simulator
683-
682+
684683 optSim = simulator;
685684 job->optSim = optSim;
686685 OptimizeMPSInitialQubitsMap (optSim, distCirc,
@@ -2048,14 +2047,14 @@ class SimpleDisconnectedNetwork : public INetwork<Time> {
20482047 singularValueThreshold.c_str ());
20492048 if (!mpsSample.empty ())
20502049 sim->Configure (" mps_sample_measure_algorithm" , mpsSample.c_str ());
2051-
2050+
20522051 sim->AllocateQubits (nrQubits);
20532052 sim->Initialize ();
20542053
20552054 OptimizeMPSInitialQubitsMap (sim, dcirc, nrQubits);
20562055 } else {
20572056 sim->AllocateQubits (nrQubits);
2058- sim->Initialize ();
2057+ sim->Initialize ();
20592058 }
20602059
20612060 if (!dontRunCircuitStart) {
@@ -2072,9 +2071,9 @@ class SimpleDisconnectedNetwork : public INetwork<Time> {
20722071
20732072 std::shared_ptr<Simulators::ISimulator> sim =
20742073 simulatorsEstimator->ChooseBestSimulator (
2075- simulatorTypes, dcirc, counts, nrQubits, nrCbits, nrResultCbits,
2076- simType, method, executed, maxBondDim, singularValueThreshold,
2077- mpsSample, GetMaxSimulators (), pauliStrings, multithreading);
2074+ simulatorTypes, dcirc, counts, nrQubits, nrCbits, nrResultCbits,
2075+ simType, method, executed, maxBondDim, singularValueThreshold,
2076+ mpsSample, GetMaxSimulators (), pauliStrings, multithreading);
20782077
20792078 if (sim) {
20802079 sim->AllocateQubits (nrQubits);
@@ -2093,7 +2092,6 @@ class SimpleDisconnectedNetwork : public INetwork<Time> {
20932092 return sim;
20942093 }
20952094
2096-
20972095 void SetInitialQubitsMapOptimization (bool optimize = true ) override {
20982096 optimizeInitialQubitsMap = optimize;
20992097 }
@@ -2124,6 +2122,26 @@ class SimpleDisconnectedNetwork : public INetwork<Time> {
21242122 return mpsOptimizationQubitsNumberThreshold;
21252123 }
21262124
2125+ void SetLookaheadDepth (int depth) override {
2126+ if (depth < 0 ) depth = std::numeric_limits<int >::max ();
2127+
2128+ lookaheadDepth = depth;
2129+ }
2130+
2131+ int GetLookaheadDepth () const override { return lookaheadDepth; }
2132+
2133+ void SetLookaheadDepthWithHeuristic (int depth) override {
2134+ if (depth < 0 ) depth = std::numeric_limits<int >::max ();
2135+
2136+ if (depth > lookaheadDepth) depth = lookaheadDepth;
2137+
2138+ lookaheadDepthWithHeuristic = depth;
2139+ }
2140+
2141+ int GetLookaheadDepthWithHeuristic () const override {
2142+ return lookaheadDepthWithHeuristic;
2143+ }
2144+
21272145 protected:
21282146 void OptimizeMPSInitialQubitsMap (
21292147 std::shared_ptr<Simulators::ISimulator> &sim,
@@ -2142,18 +2160,6 @@ class SimpleDisconnectedNetwork : public INetwork<Time> {
21422160 dcirc->ConvertForCutting (); // convert the three qubit gates
21432161 auto layers = dcirc->ToMultipleQubitsLayersNoClone ();
21442162
2145- double avgTwoQubitGatesPerLayer = 0.0 ;
2146- for (const auto &layer : layers) {
2147- int twoQubitGates = 0 ;
2148- for (const auto &op : layer->GetOperations ()) {
2149- if (op->AffectedQubits ().size () >= 2 ) {
2150- ++twoQubitGates;
2151- }
2152- }
2153- avgTwoQubitGatesPerLayer += twoQubitGates;
2154- }
2155- avgTwoQubitGatesPerLayer /= layers.size ();
2156-
21572163 Simulators::MPSDummySimulator dummySim (nrQubits);
21582164 if (!maxBondDim.empty ())
21592165 dummySim.SetMaxBondDimension (maxBondDimValue);
@@ -2168,24 +2174,43 @@ class SimpleDisconnectedNetwork : public INetwork<Time> {
21682174
21692175 if (mpsOptimizeSwaps) {
21702176 // TODO: come up with something better!
2171- int lookaheadVal = static_cast <int >(4 . * avgTwoQubitGatesPerLayer);
2172- if (lookaheadVal > 15 ) lookaheadVal = 15 ;
2177+ int lookaheadDepthLocal = lookaheadDepth;
2178+
2179+ if (lookaheadDepthLocal == std::numeric_limits<int >::max ()) {
2180+ double avgTwoQubitGatesPerLayer = 0.0 ;
2181+ for (const auto &layer : layers) {
2182+ int twoQubitGates = 0 ;
2183+ for (const auto &op : layer->GetOperations ()) {
2184+ if (op->AffectedQubits ().size () >= 2 ) {
2185+ ++twoQubitGates;
2186+ }
2187+ }
2188+ avgTwoQubitGatesPerLayer += twoQubitGates;
2189+ }
2190+ avgTwoQubitGatesPerLayer /= layers.size ();
21732191
2174- const int lookaheadDepth = layers.size () < 10 ? 0
2175- : layers.size () < 20
2176- ? static_cast <int >(lookaheadVal)
2177- : layers.size () < 35 ? 1.5 * lookaheadVal
2178- : 2 * lookaheadVal;
2192+ int lookaheadVal = static_cast <int >(4 . * avgTwoQubitGatesPerLayer);
2193+ if (lookaheadVal > 15 ) lookaheadVal = 15 ;
21792194
2180- const int lookaheadHeuristicDepth = layers.size () < 10 ? 0
2181- : layers.size () < 20
2182- ? lookaheadDepth - 1
2183- : lookaheadDepth - 2 ;
2195+ lookaheadDepthLocal = layers.size () < 10 || nrQubits <= 10 ? 0
2196+ : layers.size () < 20
2197+ ? static_cast <int >(lookaheadVal)
2198+ : layers.size () < 35 ? 1.5 * lookaheadVal
2199+ : 2 * lookaheadVal;
2200+ }
21842201
2185- sim->SetUseOptimalMeetingPosition (true );
2186- sim->SetLookaheadDepth (lookaheadDepth);
2187- sim->SetLookaheadDepthWithHeuristic (lookaheadHeuristicDepth);
2202+ int lookaheadHeuristicDepthLocal = lookaheadDepthWithHeuristic;
21882203
2204+ if (lookaheadHeuristicDepthLocal == std::numeric_limits<int >::max ())
2205+ lookaheadHeuristicDepthLocal =
2206+ layers.size () < 10 || nrQubits <= 10 ? 0
2207+ : layers.size () < 20
2208+ ? lookaheadDepthLocal - 1
2209+ : lookaheadDepthLocal - 2 ;
2210+
2211+ sim->SetUseOptimalMeetingPosition (true );
2212+ sim->SetLookaheadDepth (lookaheadDepthLocal);
2213+ sim->SetLookaheadDepthWithHeuristic (lookaheadHeuristicDepthLocal);
21892214 sim->SetUpcomingGates (dcirc->GetOperations ());
21902215 }
21912216 }
@@ -2431,6 +2456,12 @@ class SimpleDisconnectedNetwork : public INetwork<Time> {
24312456 32 ; /* *< The bond dimension threshold for using MPS optimization. */
24322457 size_t mpsOptimizationQubitsNumberThreshold =
24332458 12 ; /* *< The qubits number threshold for using MPS optimization. */
2459+
2460+ int lookaheadDepth =
2461+ std::numeric_limits<int >::max(); /* *< The lookahead depth for MPS swap
2462+ optimization. */
2463+ int lookaheadDepthWithHeuristic = std::numeric_limits<int >::max(); /* *< The
2464+ lookahead depth with heuristic for MPS swap optimization. */
24342465};
24352466
24362467} // namespace Network
0 commit comments