Skip to content

Commit

Permalink
Merge pull request #123 from quantumlib/formux
Browse files Browse the repository at this point in the history
Add a mux for ParallelFor/SequentialFor.
  • Loading branch information
sergeisakov authored Jun 4, 2020
2 parents 6670b29 + f1e7f0e commit ccca037
Show file tree
Hide file tree
Showing 29 changed files with 181 additions and 151 deletions.
4 changes: 2 additions & 2 deletions apps/qsim_amplitudes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

#include "../lib/bitstring.h"
#include "../lib/circuit_qsim_parser.h"
#include "../lib/formux.h"
#include "../lib/fuser_basic.h"
#include "../lib/gates_qsim.h"
#include "../lib/io_file.h"
#include "../lib/parfor.h"
#include "../lib/run_qsim.h"
#include "../lib/simmux.h"
#include "../lib/util.h"
Expand Down Expand Up @@ -153,7 +153,7 @@ int main(int argc, char* argv[]) {
return 1;
}

using Simulator = qsim::Simulator<ParallelFor>;
using Simulator = qsim::Simulator<For>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;

Expand Down
4 changes: 2 additions & 2 deletions apps/qsim_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#include <string>

#include "../lib/circuit_qsim_parser.h"
#include "../lib/formux.h"
#include "../lib/fuser_basic.h"
#include "../lib/gates_qsim.h"
#include "../lib/io_file.h"
#include "../lib/parfor.h"
#include "../lib/run_qsim.h"
#include "../lib/simmux.h"

Expand Down Expand Up @@ -105,7 +105,7 @@ int main(int argc, char* argv[]) {
return 1;
}

using Simulator = qsim::Simulator<ParallelFor>;
using Simulator = qsim::Simulator<For>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;
using Runner = QSimRunner<IO, BasicGateFuser<GateQSim<float>>, Simulator>;
Expand Down
8 changes: 4 additions & 4 deletions apps/qsim_von_neumann.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#include <string>

#include "../lib/circuit_qsim_parser.h"
#include "../lib/formux.h"
#include "../lib/fuser_basic.h"
#include "../lib/gates_qsim.h"
#include "../lib/io_file.h"
#include "../lib/parfor.h"
#include "../lib/run_qsim.h"
#include "../lib/simmux.h"

Expand Down Expand Up @@ -90,7 +90,7 @@ int main(int argc, char* argv[]) {
return 1;
}

using Simulator = qsim::Simulator<ParallelFor>;
using Simulator = qsim::Simulator<For>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;

Expand All @@ -105,8 +105,8 @@ int main(int argc, char* argv[]) {
};

auto size = state_space.Size(state);
double entropy = -ParallelFor::RunReduce(opt.num_threads, size, f, Op(),
state_space, state);
double entropy = -For::RunReduce(opt.num_threads, size, f, Op(),
state_space, state);
IO::messagef("entropy=%g\n", entropy);
};

Expand Down
6 changes: 3 additions & 3 deletions apps/qsimh_amplitudes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

#include "../lib/bitstring.h"
#include "../lib/circuit_qsim_parser.h"
#include "../lib/formux.h"
#include "../lib/fuser_basic.h"
#include "../lib/gates_qsim.h"
#include "../lib/io_file.h"
#include "../lib/parfor.h"
#include "../lib/run_qsimh.h"
#include "../lib/simmux.h"
#include "../lib/util.h"
Expand Down Expand Up @@ -186,9 +186,9 @@ int main(int argc, char* argv[]) {
return 1;
}

using Simulator = qsim::Simulator<ParallelFor>;
using Simulator = qsim::Simulator<For>;
using HybridSimulator = HybridSimulator<IO, GateQSim<float>, BasicGateFuser,
Simulator, ParallelFor>;
Simulator, For>;
using Runner = QSimHRunner<IO, HybridSimulator>;

Runner::Parameter param;
Expand Down
6 changes: 3 additions & 3 deletions apps/qsimh_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

#include "../lib/bitstring.h"
#include "../lib/circuit_qsim_parser.h"
#include "../lib/formux.h"
#include "../lib/fuser_basic.h"
#include "../lib/gates_qsim.h"
#include "../lib/io_file.h"
#include "../lib/parfor.h"
#include "../lib/run_qsimh.h"
#include "../lib/simmux.h"
#include "../lib/util.h"
Expand Down Expand Up @@ -151,9 +151,9 @@ int main(int argc, char* argv[]) {
bitstrings.push_back(i);
}

using Simulator = qsim::Simulator<ParallelFor>;
using Simulator = qsim::Simulator<For>;
using HybridSimulator = HybridSimulator<IO, GateQSim<float>, BasicGateFuser,
Simulator, ParallelFor>;
Simulator, For>;
using Runner = QSimHRunner<IO, HybridSimulator>;

Runner::Parameter param;
Expand Down
1 change: 1 addition & 0 deletions docs/type_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ they fulfill the same expectations.
| Op (util.h) | [`to_int` (in `Options`)](apps/qsim_amplitudes.cc) |
| ParallelFor | [`ParallelFor`](lib/parfor.h) |
| Params | Vector of `fp_type`. |
| SequentialFor | [`SequentialFor`](lib/seqfor.h) |
| Simulator | [`SimulatorAVX`](lib/simulator_avx.h) |
| State | Unique pointer to `fp_type`. |
| StateSpace | [`StateSpace`](lib/statespace.h) |
Expand Down
7 changes: 0 additions & 7 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,3 @@ Example:
./qsimh_amplitudes.x -c ../circuits/circuit_q40 -d 47 -k 0,1,2,3,4,5,6,7,8,9,10,13,14,15,16,17,23,24 -t 8 -w 1 -p 9 -r 4 -i ../circuits/bitstrings_q40_s1 -o ampl_q40_s1_w1 -v 1
...
```

# Running without AVX

SimulatorBasic can be used on older systems that do not support AVX
instructions. In this case, simulator_basic.h should be included instead of
simulator_avx.h and SimulatorAVX should be replaced by
SimulatorBasic<ParallelFor, float>.
15 changes: 15 additions & 0 deletions lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cc_library(
"bitstring.h",
"circuit_qsim_parser.h",
"circuit.h",
"formux.h",
"fuser_basic.h",
"fuser.h",
"gate.h",
Expand Down Expand Up @@ -41,6 +42,7 @@ cc_library(
hdrs = [
"circuit.h",
"circuit_qsim_parser.h",
"formux.h",
"fuser.h",
"fuser_basic.h",
"gate.h",
Expand All @@ -51,6 +53,7 @@ cc_library(
"matrix.h",
"parfor.h",
"run_qsim.h",
"seqfor.h",
"simmux.h",
"simulator_avx.h",
"simulator_basic.h",
Expand All @@ -69,6 +72,7 @@ cc_library(
hdrs = [
"circuit.h",
"circuit_qsim_parser.h",
"formux.h",
"fuser.h",
"fuser_basic.h",
"gate.h",
Expand All @@ -80,6 +84,7 @@ cc_library(
"matrix.h",
"parfor.h",
"run_qsimh.h",
"seqfor.h",
"simmux.h",
"simulator_avx.h",
"simulator_basic.h",
Expand Down Expand Up @@ -139,6 +144,16 @@ cc_library(
hdrs = ["seqfor.h"],
)

# All three state-vector simulators with multiplexer
cc_library(
name = "formux",
hdrs = ["formux.h"],
deps = [
":parfor",
":seqfor",
],
)

### Gate libraries ###

cc_library(
Expand Down
30 changes: 30 additions & 0 deletions lib/formux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef FORMUX_H_
#define FORMUX_H_

#ifdef _OPENMP
# include "parfor.h"
namespace qsim {
using For = ParallelFor;
}
#else
# include "seqfor.h"
namespace qsim {
using For = SequentialFor;
}
#endif

#endif // FORMUX_H_
7 changes: 3 additions & 4 deletions lib/hybrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace qsim {

// Hybrid Feynmann-Schrodiner simulator.
template <typename IO, typename GateT, template <typename> class FuserT,
typename Simulator, typename ParallelFor>
typename Simulator, typename For>
struct HybridSimulator final {
public:
using Gate = GateT;
Expand Down Expand Up @@ -368,9 +368,8 @@ struct HybridSimulator final {
};

// Collect results.
ParallelFor::Run(param.num_threads, results.size(), f,
sspace0, sspace1, *rstate0, *rstate1, indices,
results);
For::Run(param.num_threads, results.size(), f, sspace0, sspace1,
*rstate0, *rstate1, indices, results);
}
}

Expand Down
12 changes: 6 additions & 6 deletions lib/simmux.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
#ifdef __AVX2__
# include "simulator_avx.h"
namespace qsim {
template <typename ParallelFor>
using Simulator = SimulatorAVX<ParallelFor>;
template <typename For>
using Simulator = SimulatorAVX<For>;
}
#elif __SSE4_1__
# include "simulator_sse.h"
namespace qsim {
template <typename ParallelFor>
using Simulator = SimulatorSSE<ParallelFor>;
template <typename For>
using Simulator = SimulatorSSE<For>;
}
#else
# include "simulator_basic.h"
namespace qsim {
template <typename ParallelFor>
using Simulator = SimulatorBasic<ParallelFor>;
template <typename For>
using Simulator = SimulatorBasic<For>;
}
#endif

Expand Down
23 changes: 11 additions & 12 deletions lib/simulator_avx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
namespace qsim {

// Quantum circuit simulator with AVX vectorization.
template <typename ParallelFor>
template <typename For>
class SimulatorAVX final {
public:
using StateSpace = StateSpaceAVX<ParallelFor>;
using StateSpace = StateSpaceAVX<For>;
using State = typename StateSpace::State;
using fp_type = typename StateSpace::fp_type;

Expand Down Expand Up @@ -130,8 +130,7 @@ class SimulatorAVX final {
_mm256_store_ps(rstate + p + 8, in);
};

ParallelFor::Run(num_threads_, sizei / 16, f,
sizek, mask0, mask1, matrix, rstate);
For::Run(num_threads_, sizei / 16, f, sizek, mask0, mask1, matrix, rstate);
}

// Applies a single-qubit gate for qubit <= 2.
Expand Down Expand Up @@ -224,8 +223,8 @@ class SimulatorAVX final {
_mm256_store_ps(rstate + p + 8, in);
};

ParallelFor::Run(num_threads_, std::max(uint64_t{1}, sizei / 16), f,
q0, ml, matrix, rstate);
For::Run(num_threads_, std::max(uint64_t{1}, sizei / 16), f, q0, ml,
matrix, rstate);
}

// Applies two-qubit gate for qubit0 > 2 and qubit1 > 2.
Expand Down Expand Up @@ -377,8 +376,8 @@ class SimulatorAVX final {
_mm256_store_ps(rstate + p + 8, in);
};

ParallelFor::Run(num_threads_, sizei / 16, f,
sizej, sizek, mask0, mask1, mask2, matrix, rstate);
For::Run(num_threads_, sizei / 16, f, sizej, sizek, mask0, mask1, mask2,
matrix, rstate);
}

// Applies a two-qubit gate for qubit0 <= 2 and qubit1 > 2.
Expand Down Expand Up @@ -587,8 +586,8 @@ class SimulatorAVX final {
_mm256_store_ps(rstate + p + 8, in);
};

ParallelFor::Run(num_threads_, sizei / 16, f,
sizej, mask0, mask1, q0, ml, matrix, rstate);
For::Run(num_threads_, sizei / 16, f, sizej, mask0, mask1, q0, ml,
matrix, rstate);
}

// Applies a two-qubit gate for qubit0 <= 2 and qubit1 <= 2.
Expand Down Expand Up @@ -815,8 +814,8 @@ class SimulatorAVX final {
_mm256_store_ps(rstate + p + 8, in);
};

ParallelFor::Run(num_threads_, std::max(uint64_t{1}, sizei / 16), f,
q, ml1, ml2, ml3, matrix, rstate);
For::Run(num_threads_, std::max(uint64_t{1}, sizei / 16), f, q,
ml1, ml2, ml3, matrix, rstate);
}

unsigned num_qubits_;
Expand Down
11 changes: 5 additions & 6 deletions lib/simulator_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
namespace qsim {

// Quantim circuit simulator without vectorization.
template <typename ParallelFor, typename FP = float>
template <typename For, typename FP = float>
class SimulatorBasic final {
public:
using StateSpace = StateSpaceBasic<ParallelFor, FP>;
using StateSpace = StateSpaceBasic<For, FP>;
using State = typename StateSpace::State;
using fp_type = typename StateSpace::fp_type;

Expand Down Expand Up @@ -66,8 +66,7 @@ class SimulatorBasic final {
rstate[si1 + 1] = s0r * u[5] + s0i * u[4] + s1r * u[7] + s1i * u[6];
};

ParallelFor::Run(num_threads_, sizei / 2, f,
sizek, mask0, mask1, matrix, rstate);
For::Run(num_threads_, sizei / 2, f, sizek, mask0, mask1, matrix, rstate);
}

/**
Expand Down Expand Up @@ -130,8 +129,8 @@ class SimulatorBasic final {
+ s2r * u[29] + s2i * u[28] + s3r * u[31] + s3i * u[30];
};

ParallelFor::Run(num_threads_, sizei / 2, f,
sizej, sizek, mask0, mask1, mask2, matrix, rstate);
For::Run(num_threads_, sizei / 2, f, sizej, sizek, mask0, mask1, mask2,
matrix, rstate);
}

private:
Expand Down
Loading

0 comments on commit ccca037

Please sign in to comment.