Skip to content

Commit f8e791a

Browse files
authored
Merge pull request #245 from quantumlib/multi-qubit-fuser
Add multi-qubit fuser.
2 parents e939909 + 114e868 commit f8e791a

File tree

12 files changed

+1509
-31
lines changed

12 files changed

+1509
-31
lines changed

apps/qsim_amplitudes.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "../lib/bitstring.h"
2525
#include "../lib/circuit_qsim_parser.h"
2626
#include "../lib/formux.h"
27-
#include "../lib/fuser_basic.h"
27+
#include "../lib/fuser_mqubit.h"
2828
#include "../lib/gates_qsim.h"
2929
#include "../lib/io_file.h"
3030
#include "../lib/run_qsim.h"
@@ -34,7 +34,7 @@
3434
constexpr char usage[] = "usage:\n ./qsim_amplitudes -c circuit_file "
3535
"-d times_to_save_results -i input_files "
3636
"-o output_files -s seed -t num_threads "
37-
"-v verbosity\n";
37+
"-f max_fused_size -v verbosity\n";
3838

3939
struct Options {
4040
std::string circuit_file;
@@ -43,6 +43,7 @@ struct Options {
4343
std::vector<std::string> output_files;
4444
unsigned seed = 1;
4545
unsigned num_threads = 1;
46+
unsigned max_fused_size = 2;
4647
unsigned verbosity = 0;
4748
};
4849

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

58-
while ((k = getopt(argc, argv, "c:d:i:s:o:t:v:")) != -1) {
59+
while ((k = getopt(argc, argv, "c:d:i:s:o:t:f:v:")) != -1) {
5960
switch (k) {
6061
case 'c':
6162
opt.circuit_file = optarg;
@@ -75,6 +76,9 @@ Options GetOptions(int argc, char* argv[]) {
7576
case 't':
7677
opt.num_threads = std::atoi(optarg);
7778
break;
79+
case 'f':
80+
opt.max_fused_size = std::atoi(optarg);
81+
break;
7882
case 'v':
7983
opt.verbosity = std::atoi(optarg);
8084
break;
@@ -161,6 +165,8 @@ int main(int argc, char* argv[]) {
161165
using Simulator = qsim::Simulator<For>;
162166
using StateSpace = Simulator::StateSpace;
163167
using State = StateSpace::State;
168+
using Fuser = MultiQubitGateFuser<IO, GateQSim<float>>;
169+
using Runner = QSimRunner<IO, Fuser, Simulator>;
164170

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

175-
using Runner = QSimRunner<IO, BasicGateFuser<IO, GateQSim<float>>, Simulator>;
176-
177181
Runner::Parameter param;
182+
param.max_fused_size = opt.max_fused_size;
178183
param.seed = opt.seed;
179184
param.num_threads = opt.num_threads;
180185
param.verbosity = opt.verbosity;

apps/qsim_base.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include "../lib/circuit_qsim_parser.h"
2323
#include "../lib/formux.h"
24-
#include "../lib/fuser_basic.h"
24+
#include "../lib/fuser_mqubit.h"
2525
#include "../lib/gates_qsim.h"
2626
#include "../lib/io_file.h"
2727
#include "../lib/run_qsim.h"
@@ -32,18 +32,20 @@ struct Options {
3232
unsigned maxtime = std::numeric_limits<unsigned>::max();
3333
unsigned seed = 1;
3434
unsigned num_threads = 1;
35+
unsigned max_fused_size = 2;
3536
unsigned verbosity = 0;
3637
};
3738

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

4244
Options opt;
4345

4446
int k;
4547

46-
while ((k = getopt(argc, argv, "c:d:s:t:v:")) != -1) {
48+
while ((k = getopt(argc, argv, "c:d:s:t:f:v:")) != -1) {
4749
switch (k) {
4850
case 'c':
4951
opt.circuit_file = optarg;
@@ -57,6 +59,9 @@ Options GetOptions(int argc, char* argv[]) {
5759
case 't':
5860
opt.num_threads = std::atoi(optarg);
5961
break;
62+
case 'f':
63+
opt.max_fused_size = std::atoi(optarg);
64+
break;
6065
case 'v':
6166
opt.verbosity = std::atoi(optarg);
6267
break;
@@ -112,7 +117,8 @@ int main(int argc, char* argv[]) {
112117
using Simulator = qsim::Simulator<For>;
113118
using StateSpace = Simulator::StateSpace;
114119
using State = StateSpace::State;
115-
using Runner = QSimRunner<IO, BasicGateFuser<IO, GateQSim<float>>, Simulator>;
120+
using Fuser = MultiQubitGateFuser<IO, GateQSim<float>>;
121+
using Runner = QSimRunner<IO, Fuser, Simulator>;
116122

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

127133
Runner::Parameter param;
134+
param.max_fused_size = opt.max_fused_size;
128135
param.seed = opt.seed;
129136
param.num_threads = opt.num_threads;
130137
param.verbosity = opt.verbosity;

apps/qsim_von_neumann.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include "../lib/circuit_qsim_parser.h"
2525
#include "../lib/formux.h"
26-
#include "../lib/fuser_basic.h"
26+
#include "../lib/fuser_mqubit.h"
2727
#include "../lib/gates_qsim.h"
2828
#include "../lib/io_file.h"
2929
#include "../lib/run_qsim.h"
@@ -34,18 +34,20 @@ struct Options {
3434
unsigned maxtime = std::numeric_limits<unsigned>::max();
3535
unsigned seed = 1;
3636
unsigned num_threads = 1;
37+
unsigned max_fused_size = 2;
3738
unsigned verbosity = 0;
3839
};
3940

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

4446
Options opt;
4547

4648
int k;
4749

48-
while ((k = getopt(argc, argv, "c:d:s:t:v:")) != -1) {
50+
while ((k = getopt(argc, argv, "c:d:s:t:f:v:")) != -1) {
4951
switch (k) {
5052
case 'c':
5153
opt.circuit_file = optarg;
@@ -59,6 +61,9 @@ Options GetOptions(int argc, char* argv[]) {
5961
case 't':
6062
opt.num_threads = std::atoi(optarg);
6163
break;
64+
case 'f':
65+
opt.max_fused_size = std::atoi(optarg);
66+
break;
6267
case 'v':
6368
opt.verbosity = std::atoi(optarg);
6469
break;
@@ -97,6 +102,8 @@ int main(int argc, char* argv[]) {
97102
using Simulator = qsim::Simulator<For>;
98103
using StateSpace = Simulator::StateSpace;
99104
using State = StateSpace::State;
105+
using Fuser = MultiQubitGateFuser<IO, GateQSim<float>>;
106+
using Runner = QSimRunner<IO, Fuser, Simulator>;
100107

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

116-
using Runner = QSimRunner<IO, BasicGateFuser<IO, GateQSim<float>>, Simulator>;
117-
118123
Runner::Parameter param;
124+
param.max_fused_size = opt.max_fused_size;
119125
param.seed = opt.seed;
120126
param.num_threads = opt.num_threads;
121127
param.verbosity = opt.verbosity;

docs/usage.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ Sample circuits are provided in [circuits](/circuits).
1111
## qsim_base usage
1212

1313
```
14-
./qsim_base.x -c circuit_file -d maxtime -t num_threads -v verbosity
14+
./qsim_base.x -c circuit_file -d maxtime -t num_threads -f max_fused_size -v verbosity
1515
```
1616

17-
| Flag | Description |
17+
| Flag | Description |
1818
|-------|------------|
19-
|`-c circuit_file` | circuit file to run|
19+
|`-c circuit_file` | circuit file to run|
2020
|`-d maxtime` | maximum time |
2121
|`-t num_threads` | number of threads to use|
22+
|`-f max_fused_size` | maximum fused gate size|
2223
|`-v verbosity` | verbosity level (0,1,>1)|
2324

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

3435
```
35-
./qsim_von_neumann.x -c circuit_file -d maxtime -t num_threads -v verbosity
36+
./qsim_von_neumann.x -c circuit_file -d maxtime -t num_threads -f max_fused_size -v verbosity
3637
```
3738

3839

39-
| Flag | Description |
40+
| Flag | Description |
4041
|-------|------------|
41-
|`-c circuit_file` | circuit file to run|
42+
|`-c circuit_file` | circuit file to run|
4243
|`-d maxtime` | maximum time |
4344
|`-t num_threads` | number of threads to use|
45+
|`-f max_fused_size` | maximum fused gate size|
4446
|`-v verbosity` | verbosity level (0,1,>1)|
4547

4648
qsim_von_neumann computes all the amplitudes and calculates the von Neumann
@@ -58,17 +60,19 @@ Example:
5860
./qsim_amplitudes.x -c circuit_file \
5961
-d times_to_save_results \
6062
-i input_files \
61-
-o output_files \
63+
-o output_files \
64+
-f max_fused_size \
6265
-t num_threads -v verbosity
6366
```
6467

65-
| Flag | Description |
68+
| Flag | Description |
6669
|-------|------------|
67-
|`-c circuit_file` | circuit file to run|
70+
|`-c circuit_file` | circuit file to run|
6871
|`-d times_to_save_results` | comma-separated list of circuit times to save results at|
6972
|`-i input_files` | comma-separated list of bitstring input files|
7073
|`-o output_files` | comma-separated list of amplitude output files|
7174
|`-t num_threads` | number of threads to use|
75+
|`-f max_fused_size` | maximum fused gate size|
7276
|`-v verbosity` | verbosity level (0,1,>1)|
7377

7478
qsim_amplitudes reads input files of bitstrings, computes the corresponding
@@ -94,9 +98,9 @@ Example:
9498
-t num_threads -v verbosity
9599
```
96100

97-
| Flag | Description |
101+
| Flag | Description |
98102
|-------|------------|
99-
|`-c circuit_file` | circuit file to run|
103+
|`-c circuit_file` | circuit file to run|
100104
|`-d maxtime` | maximum time |
101105
|`-k part1_qubits` | comma-separated list of qubit indices for part 1 |
102106
|`-w prefix`| prefix value |
@@ -173,9 +177,9 @@ maximum "time".
173177
-t num_threads -v verbosity
174178
```
175179

176-
| Flag | Description |
180+
| Flag | Description |
177181
|-------|------------|
178-
|`-c circuit_file` | circuit file to run|
182+
|`-c circuit_file` | circuit file to run|
179183
|`-d maxtime` | maximum time |
180184
|`-k part1_qubits` | comma-separated list of qubit indices for part 1 |
181185
|`-w prefix`| prefix value |

lib/BUILD

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ cc_library(
1111
"circuit_qsim_parser.h",
1212
"circuit.h",
1313
"formux.h",
14-
"fuser_basic.h",
1514
"fuser.h",
15+
"fuser_basic.h",
16+
"fuser_mqubit.h",
1617
"gate.h",
1718
"gate_appl.h",
1819
"gates_cirq.h",
@@ -52,6 +53,7 @@ cc_library(
5253
"formux.h",
5354
"fuser.h",
5455
"fuser_basic.h",
56+
"fuser_mqubit.h",
5557
"gate.h",
5658
"gate_appl.h",
5759
"gates_qsim.h",
@@ -88,6 +90,7 @@ cc_library(
8890
"formux.h",
8991
"fuser.h",
9092
"fuser_basic.h",
93+
"fuser_mqubit.h",
9194
"gate.h",
9295
"gate_appl.h",
9396
"gates_qsim.h",
@@ -241,6 +244,15 @@ cc_library(
241244
],
242245
)
243246

247+
cc_library(
248+
name = "fuser_mqubit",
249+
hdrs = ["fuser_mqubit.h"],
250+
deps = [
251+
":fuser",
252+
":gate",
253+
],
254+
)
255+
244256
### Helper libraries to run qsim and qsimh ###
245257

246258
cc_library(

lib/fuser_basic.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ struct BasicGateFuser final {
3535
* User-specified parameters for gate fusion.
3636
* BasicGateFuser does not use any parameters.
3737
*/
38-
struct Parameter {};
38+
struct Parameter {
39+
unsigned verbosity = 0;
40+
};
3941

4042
/**
4143
* Stores ordered sets of gates, each acting on two qubits, that can be

0 commit comments

Comments
 (0)