Skip to content

Commit

Permalink
Merge pull request quantumlib#245 from quantumlib/multi-qubit-fuser
Browse files Browse the repository at this point in the history
Add multi-qubit fuser.
  • Loading branch information
sergeisakov authored Nov 25, 2020
2 parents e939909 + 114e868 commit f8e791a
Show file tree
Hide file tree
Showing 12 changed files with 1,509 additions and 31 deletions.
15 changes: 10 additions & 5 deletions apps/qsim_amplitudes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "../lib/bitstring.h"
#include "../lib/circuit_qsim_parser.h"
#include "../lib/formux.h"
#include "../lib/fuser_basic.h"
#include "../lib/fuser_mqubit.h"
#include "../lib/gates_qsim.h"
#include "../lib/io_file.h"
#include "../lib/run_qsim.h"
Expand All @@ -34,7 +34,7 @@
constexpr char usage[] = "usage:\n ./qsim_amplitudes -c circuit_file "
"-d times_to_save_results -i input_files "
"-o output_files -s seed -t num_threads "
"-v verbosity\n";
"-f max_fused_size -v verbosity\n";

struct Options {
std::string circuit_file;
Expand All @@ -43,6 +43,7 @@ struct Options {
std::vector<std::string> output_files;
unsigned seed = 1;
unsigned num_threads = 1;
unsigned max_fused_size = 2;
unsigned verbosity = 0;
};

Expand All @@ -55,7 +56,7 @@ Options GetOptions(int argc, char* argv[]) {
return std::atoi(word.c_str());
};

while ((k = getopt(argc, argv, "c:d:i:s:o:t:v:")) != -1) {
while ((k = getopt(argc, argv, "c:d:i:s:o:t:f:v:")) != -1) {
switch (k) {
case 'c':
opt.circuit_file = optarg;
Expand All @@ -75,6 +76,9 @@ Options GetOptions(int argc, char* argv[]) {
case 't':
opt.num_threads = std::atoi(optarg);
break;
case 'f':
opt.max_fused_size = std::atoi(optarg);
break;
case 'v':
opt.verbosity = std::atoi(optarg);
break;
Expand Down Expand Up @@ -161,6 +165,8 @@ int main(int argc, char* argv[]) {
using Simulator = qsim::Simulator<For>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;
using Fuser = MultiQubitGateFuser<IO, GateQSim<float>>;
using Runner = QSimRunner<IO, Fuser, Simulator>;

auto measure = [&opt, &circuit](
unsigned k, const StateSpace& state_space, const State& state) {
Expand All @@ -172,9 +178,8 @@ int main(int argc, char* argv[]) {
}
};

using Runner = QSimRunner<IO, BasicGateFuser<IO, GateQSim<float>>, Simulator>;

Runner::Parameter param;
param.max_fused_size = opt.max_fused_size;
param.seed = opt.seed;
param.num_threads = opt.num_threads;
param.verbosity = opt.verbosity;
Expand Down
15 changes: 11 additions & 4 deletions apps/qsim_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "../lib/circuit_qsim_parser.h"
#include "../lib/formux.h"
#include "../lib/fuser_basic.h"
#include "../lib/fuser_mqubit.h"
#include "../lib/gates_qsim.h"
#include "../lib/io_file.h"
#include "../lib/run_qsim.h"
Expand All @@ -32,18 +32,20 @@ struct Options {
unsigned maxtime = std::numeric_limits<unsigned>::max();
unsigned seed = 1;
unsigned num_threads = 1;
unsigned max_fused_size = 2;
unsigned verbosity = 0;
};

Options GetOptions(int argc, char* argv[]) {
constexpr char usage[] = "usage:\n ./qsim_base -c circuit -d maxtime "
"-s seed -t threads -v verbosity\n";
"-s seed -t threads -f max_fused_size "
"-v verbosity\n";

Options opt;

int k;

while ((k = getopt(argc, argv, "c:d:s:t:v:")) != -1) {
while ((k = getopt(argc, argv, "c:d:s:t:f:v:")) != -1) {
switch (k) {
case 'c':
opt.circuit_file = optarg;
Expand All @@ -57,6 +59,9 @@ Options GetOptions(int argc, char* argv[]) {
case 't':
opt.num_threads = std::atoi(optarg);
break;
case 'f':
opt.max_fused_size = std::atoi(optarg);
break;
case 'v':
opt.verbosity = std::atoi(optarg);
break;
Expand Down Expand Up @@ -112,7 +117,8 @@ int main(int argc, char* argv[]) {
using Simulator = qsim::Simulator<For>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;
using Runner = QSimRunner<IO, BasicGateFuser<IO, GateQSim<float>>, Simulator>;
using Fuser = MultiQubitGateFuser<IO, GateQSim<float>>;
using Runner = QSimRunner<IO, Fuser, Simulator>;

StateSpace state_space(opt.num_threads);
State state = state_space.Create(circuit.num_qubits);
Expand All @@ -125,6 +131,7 @@ int main(int argc, char* argv[]) {
state_space.SetStateZero(state);

Runner::Parameter param;
param.max_fused_size = opt.max_fused_size;
param.seed = opt.seed;
param.num_threads = opt.num_threads;
param.verbosity = opt.verbosity;
Expand Down
16 changes: 11 additions & 5 deletions apps/qsim_von_neumann.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include "../lib/circuit_qsim_parser.h"
#include "../lib/formux.h"
#include "../lib/fuser_basic.h"
#include "../lib/fuser_mqubit.h"
#include "../lib/gates_qsim.h"
#include "../lib/io_file.h"
#include "../lib/run_qsim.h"
Expand All @@ -34,18 +34,20 @@ struct Options {
unsigned maxtime = std::numeric_limits<unsigned>::max();
unsigned seed = 1;
unsigned num_threads = 1;
unsigned max_fused_size = 2;
unsigned verbosity = 0;
};

Options GetOptions(int argc, char* argv[]) {
constexpr char usage[] = "usage:\n ./qsim_von_neumann -c circuit -d maxtime "
"-s seed -t threads -v verbosity\n";
"-s seed -t threads -f max_fused_size "
"-v verbosity\n";

Options opt;

int k;

while ((k = getopt(argc, argv, "c:d:s:t:v:")) != -1) {
while ((k = getopt(argc, argv, "c:d:s:t:f:v:")) != -1) {
switch (k) {
case 'c':
opt.circuit_file = optarg;
Expand All @@ -59,6 +61,9 @@ Options GetOptions(int argc, char* argv[]) {
case 't':
opt.num_threads = std::atoi(optarg);
break;
case 'f':
opt.max_fused_size = std::atoi(optarg);
break;
case 'v':
opt.verbosity = std::atoi(optarg);
break;
Expand Down Expand Up @@ -97,6 +102,8 @@ int main(int argc, char* argv[]) {
using Simulator = qsim::Simulator<For>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;
using Fuser = MultiQubitGateFuser<IO, GateQSim<float>>;
using Runner = QSimRunner<IO, Fuser, Simulator>;

auto measure = [&opt, &circuit](
unsigned k, const StateSpace& state_space, const State& state) {
Expand All @@ -113,9 +120,8 @@ int main(int argc, char* argv[]) {
IO::messagef("entropy=%g\n", entropy);
};

using Runner = QSimRunner<IO, BasicGateFuser<IO, GateQSim<float>>, Simulator>;

Runner::Parameter param;
param.max_fused_size = opt.max_fused_size;
param.seed = opt.seed;
param.num_threads = opt.num_threads;
param.verbosity = opt.verbosity;
Expand Down
30 changes: 17 additions & 13 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ Sample circuits are provided in [circuits](/circuits).
## qsim_base usage

```
./qsim_base.x -c circuit_file -d maxtime -t num_threads -v verbosity
./qsim_base.x -c circuit_file -d maxtime -t num_threads -f max_fused_size -v verbosity
```

| Flag | Description |
| Flag | Description |
|-------|------------|
|`-c circuit_file` | circuit file to run|
|`-c circuit_file` | circuit file to run|
|`-d maxtime` | maximum time |
|`-t num_threads` | number of threads to use|
|`-f max_fused_size` | maximum fused gate size|
|`-v verbosity` | verbosity level (0,1,>1)|

qsim_base computes all the amplitudes and just prints the first eight of them
Expand All @@ -32,15 +33,16 @@ Example:
## qsim_von_neumann usage

```
./qsim_von_neumann.x -c circuit_file -d maxtime -t num_threads -v verbosity
./qsim_von_neumann.x -c circuit_file -d maxtime -t num_threads -f max_fused_size -v verbosity
```


| Flag | Description |
| Flag | Description |
|-------|------------|
|`-c circuit_file` | circuit file to run|
|`-c circuit_file` | circuit file to run|
|`-d maxtime` | maximum time |
|`-t num_threads` | number of threads to use|
|`-f max_fused_size` | maximum fused gate size|
|`-v verbosity` | verbosity level (0,1,>1)|

qsim_von_neumann computes all the amplitudes and calculates the von Neumann
Expand All @@ -58,17 +60,19 @@ Example:
./qsim_amplitudes.x -c circuit_file \
-d times_to_save_results \
-i input_files \
-o output_files \
-o output_files \
-f max_fused_size \
-t num_threads -v verbosity
```

| Flag | Description |
| Flag | Description |
|-------|------------|
|`-c circuit_file` | circuit file to run|
|`-c circuit_file` | circuit file to run|
|`-d times_to_save_results` | comma-separated list of circuit times to save results at|
|`-i input_files` | comma-separated list of bitstring input files|
|`-o output_files` | comma-separated list of amplitude output files|
|`-t num_threads` | number of threads to use|
|`-f max_fused_size` | maximum fused gate size|
|`-v verbosity` | verbosity level (0,1,>1)|

qsim_amplitudes reads input files of bitstrings, computes the corresponding
Expand All @@ -94,9 +98,9 @@ Example:
-t num_threads -v verbosity
```

| Flag | Description |
| Flag | Description |
|-------|------------|
|`-c circuit_file` | circuit file to run|
|`-c circuit_file` | circuit file to run|
|`-d maxtime` | maximum time |
|`-k part1_qubits` | comma-separated list of qubit indices for part 1 |
|`-w prefix`| prefix value |
Expand Down Expand Up @@ -173,9 +177,9 @@ maximum "time".
-t num_threads -v verbosity
```

| Flag | Description |
| Flag | Description |
|-------|------------|
|`-c circuit_file` | circuit file to run|
|`-c circuit_file` | circuit file to run|
|`-d maxtime` | maximum time |
|`-k part1_qubits` | comma-separated list of qubit indices for part 1 |
|`-w prefix`| prefix value |
Expand Down
14 changes: 13 additions & 1 deletion lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ cc_library(
"circuit_qsim_parser.h",
"circuit.h",
"formux.h",
"fuser_basic.h",
"fuser.h",
"fuser_basic.h",
"fuser_mqubit.h",
"gate.h",
"gate_appl.h",
"gates_cirq.h",
Expand Down Expand Up @@ -52,6 +53,7 @@ cc_library(
"formux.h",
"fuser.h",
"fuser_basic.h",
"fuser_mqubit.h",
"gate.h",
"gate_appl.h",
"gates_qsim.h",
Expand Down Expand Up @@ -88,6 +90,7 @@ cc_library(
"formux.h",
"fuser.h",
"fuser_basic.h",
"fuser_mqubit.h",
"gate.h",
"gate_appl.h",
"gates_qsim.h",
Expand Down Expand Up @@ -241,6 +244,15 @@ cc_library(
],
)

cc_library(
name = "fuser_mqubit",
hdrs = ["fuser_mqubit.h"],
deps = [
":fuser",
":gate",
],
)

### Helper libraries to run qsim and qsimh ###

cc_library(
Expand Down
4 changes: 3 additions & 1 deletion lib/fuser_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ struct BasicGateFuser final {
* User-specified parameters for gate fusion.
* BasicGateFuser does not use any parameters.
*/
struct Parameter {};
struct Parameter {
unsigned verbosity = 0;
};

/**
* Stores ordered sets of gates, each acting on two qubits, that can be
Expand Down
Loading

0 comments on commit f8e791a

Please sign in to comment.