2424#include " ../lib/fuser_mqubit.h"
2525#include " ../lib/io_file.h"
2626#include " ../lib/operation.h"
27+ #include " ../lib/qubit_remap.h"
2728#include " ../lib/run_qsim.h"
2829#include " ../lib/simmux.h"
2930#include " ../lib/util_cpu.h"
3031
3132constexpr char usage[] = " usage:\n ./qsim_base -c circuit -d maxtime "
3233 " -s seed -t threads -f max_fused_size "
33- " -v verbosity -z\n " ;
34+ " -v verbosity -r - z\n " ;
3435
3536struct Options {
3637 std::string circuit_file;
@@ -39,6 +40,7 @@ struct Options {
3940 unsigned num_threads = 1 ;
4041 unsigned max_fused_size = 2 ;
4142 unsigned verbosity = 0 ;
43+ bool cache_local_remap = false ;
4244 bool denormals_are_zeros = false ;
4345};
4446
@@ -47,7 +49,7 @@ Options GetOptions(int argc, char* argv[]) {
4749
4850 int k;
4951
50- while ((k = getopt (argc, argv, " c:d:s:t:f:v:z " )) != -1 ) {
52+ while ((k = getopt (argc, argv, " c:d:s:t:f:v:rz " )) != -1 ) {
5153 switch (k) {
5254 case ' c' :
5355 opt.circuit_file = optarg;
@@ -67,6 +69,9 @@ Options GetOptions(int argc, char* argv[]) {
6769 case ' v' :
6870 opt.verbosity = std::atoi (optarg);
6971 break ;
72+ case ' r' :
73+ opt.cache_local_remap = true ;
74+ break ;
7075 case ' z' :
7176 opt.denormals_are_zeros = true ;
7277 break ;
@@ -91,7 +96,8 @@ bool ValidateOptions(const Options& opt) {
9196
9297template <typename StateSpace, typename State>
9398void PrintAmplitudes (
94- unsigned num_qubits, const StateSpace& state_space, const State& state) {
99+ unsigned num_qubits, const StateSpace& state_space, const State& state,
100+ const qsim::qubit_remap::QubitMap& logical_to_physical = {}) {
95101 static constexpr char const * bits[8 ] = {
96102 " 000" , " 001" , " 010" , " 011" , " 100" , " 101" , " 110" , " 111" ,
97103 };
@@ -100,7 +106,9 @@ void PrintAmplitudes(
100106 unsigned s = 3 - std::min (unsigned {3 }, num_qubits);
101107
102108 for (uint64_t i = 0 ; i < size; ++i) {
103- auto a = state_space.GetAmpl (state, i);
109+ uint64_t physical_i =
110+ qsim::qubit_remap::LogicalToPhysicalIndex (i, logical_to_physical);
111+ auto a = state_space.GetAmpl (state, physical_i);
104112 qsim::IO::messagef (" %s:%16.8g%16.8g%16.8g\n " ,
105113 bits[i] + s, std::real (a), std::imag (a), std::norm (a));
106114 }
@@ -141,29 +149,37 @@ int main(int argc, char* argv[]) {
141149 unsigned num_threads;
142150 };
143151
144- using Simulator = Factory::Simulator;
145- using StateSpace = Simulator::StateSpace;
152+ using StateSpace = Factory::StateSpace;
146153 using State = StateSpace::State;
147154 using Fuser = MultiQubitGateFuser<IO >;
148155 using Runner = QSimRunner<IO , Fuser, Factory>;
149156
150- StateSpace state_space = Factory (opt.num_threads ).CreateStateSpace ();
157+ Factory factory (opt.num_threads );
158+ StateSpace state_space = factory.CreateStateSpace ();
151159 State state = state_space.Create (circuit.num_qubits );
152160
153161 if (state_space.IsNull (state)) {
154162 IO::errorf (" not enough memory: is the number of qubits too large?\n " );
155163 return 1 ;
156164 }
157165
166+ qubit_remap::QubitMap logical_to_physical;
167+
158168 state_space.SetStateZero (state);
159169
160170 Runner::Parameter param;
161171 param.max_fused_size = opt.max_fused_size ;
162172 param.seed = opt.seed ;
163173 param.verbosity = opt.verbosity ;
174+ param.cache_local_remap = opt.cache_local_remap ;
175+
176+ auto simulator = factory.CreateSimulator ();
177+ bool ok = Runner::Run (param, circuit, state_space, simulator, state,
178+ logical_to_physical);
164179
165- if (Runner::Run (param, Factory (opt.num_threads ), circuit, state)) {
166- PrintAmplitudes (circuit.num_qubits , state_space, state);
180+ if (ok) {
181+ PrintAmplitudes (circuit.num_qubits , state_space, state,
182+ logical_to_physical);
167183 }
168184
169185 return 0 ;
0 commit comments