Skip to content

Commit

Permalink
Merge pull request #141 from quantumlib/measurements
Browse files Browse the repository at this point in the history
Add measurement gate.
  • Loading branch information
sergeisakov authored Jul 6, 2020
2 parents 2c854fd + 0244439 commit 8ad553c
Show file tree
Hide file tree
Showing 32 changed files with 1,188 additions and 266 deletions.
12 changes: 9 additions & 3 deletions apps/qsim_amplitudes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@

constexpr char usage[] = "usage:\n ./qsim_amplitudes -c circuit_file "
"-d times_to_save_results -i input_files "
"-o output_files -t num_threads -v verbosity\n";
"-o output_files -s seed -t num_threads "
"-v verbosity\n";

struct Options {
std::string circuit_file;
std::vector<unsigned> times = {std::numeric_limits<unsigned>::max()};
std::vector<std::string> input_files;
std::vector<std::string> output_files;
unsigned seed = 1;
unsigned num_threads = 1;
unsigned verbosity = 0;
};
Expand All @@ -53,7 +55,7 @@ Options GetOptions(int argc, char* argv[]) {
return std::atoi(word.c_str());
};

while ((k = getopt(argc, argv, "c:d:i:o:t:v:")) != -1) {
while ((k = getopt(argc, argv, "c:d:i:s:o:t:v:")) != -1) {
switch (k) {
case 'c':
opt.circuit_file = optarg;
Expand All @@ -67,6 +69,9 @@ Options GetOptions(int argc, char* argv[]) {
case 'o':
qsim::SplitString(optarg, ',', opt.output_files);
break;
case 's':
opt.seed = std::atoi(optarg);
break;
case 't':
opt.num_threads = std::atoi(optarg);
break;
Expand Down Expand Up @@ -167,9 +172,10 @@ int main(int argc, char* argv[]) {
}
};

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

Runner::Parameter param;
param.seed = opt.seed;
param.num_threads = opt.num_threads;
param.verbosity = opt.verbosity;
Runner::Run(param, opt.times, circuit, measure);
Expand Down
13 changes: 9 additions & 4 deletions apps/qsim_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,30 @@
struct Options {
std::string circuit_file;
unsigned maxtime = std::numeric_limits<unsigned>::max();
unsigned seed = 1;
unsigned num_threads = 1;
unsigned verbosity = 0;
};

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

Options opt;

int k;

while ((k = getopt(argc, argv, "c:d:t:v:")) != -1) {
while ((k = getopt(argc, argv, "c:d:s:t:v:")) != -1) {
switch (k) {
case 'c':
opt.circuit_file = optarg;
break;
case 'd':
opt.maxtime = std::atoi(optarg);
break;
case 's':
opt.seed = std::atoi(optarg);
break;
case 't':
opt.num_threads = std::atoi(optarg);
break;
Expand Down Expand Up @@ -81,7 +85,7 @@ void PrintAmplitudes(
"000", "001", "010", "011", "100", "101", "110", "111",
};

uint64_t size = std::min(uint64_t{8}, state_space.Size(state));
uint64_t size = std::min(uint64_t{8}, state_space.Size());
unsigned s = 3 - std::min(unsigned{3}, num_qubits);

for (uint64_t i = 0; i < size; ++i) {
Expand All @@ -108,7 +112,7 @@ int main(int argc, char* argv[]) {
using Simulator = qsim::Simulator<For>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;
using Runner = QSimRunner<IO, BasicGateFuser<GateQSim<float>>, Simulator>;
using Runner = QSimRunner<IO, BasicGateFuser<IO, GateQSim<float>>, Simulator>;

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

Runner::Parameter param;
param.seed = opt.seed;
param.num_threads = opt.num_threads;
param.verbosity = opt.verbosity;

Expand Down
16 changes: 10 additions & 6 deletions apps/qsim_von_neumann.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,30 @@
struct Options {
std::string circuit_file;
unsigned maxtime = std::numeric_limits<unsigned>::max();
unsigned seed = 1;
unsigned num_threads = 1;
unsigned verbosity = 0;
};

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

Options opt;

int k;

while ((k = getopt(argc, argv, "c:d:t:v:")) != -1) {
while ((k = getopt(argc, argv, "c:d:s:t:v:")) != -1) {
switch (k) {
case 'c':
opt.circuit_file = optarg;
break;
case 'd':
opt.maxtime = std::atoi(optarg);
break;
case 's':
opt.seed = std::atoi(optarg);
break;
case 't':
opt.num_threads = std::atoi(optarg);
break;
Expand Down Expand Up @@ -104,15 +108,15 @@ int main(int argc, char* argv[]) {
return p != 0 ? p * std::log(p) : 0;
};

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

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

Runner::Parameter param;
param.seed = opt.seed;
param.num_threads = opt.num_threads;
param.verbosity = opt.verbosity;

Expand Down
4 changes: 4 additions & 0 deletions docs/input_format.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ fSim(&theta;, &phi;) | time fs qubi
CPhase(&phi;) | time cp qubit1 qubit2 phi | 6 cp 0 1 0.78
Identity (1-qubit) | time id1 qubit | 7 id1 0
Identity (2-qubit) | time id2 qubit | 8 id2 0 1
Measurement (n-qubit) | time m qubit1 qubit2 ... | 9 m 0 1 2 3

Gate times should be ordered. Measurement gates with equal times get fused
together.
7 changes: 6 additions & 1 deletion lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ cc_library(
cc_library(
name = "fuser_basic",
hdrs = ["fuser_basic.h"],
deps = [":fuser"],
deps = [
":fuser",
":gate",
],
)

### Helper libraries to run qsim and qsimh ###
Expand All @@ -216,6 +219,7 @@ cc_library(
name = "run_qsim",
hdrs = ["run_qsim.h"],
deps = [
":gate",
":gate_appl",
":util",
],
Expand All @@ -235,6 +239,7 @@ cc_library(
cc_library(
name = "statespace",
hdrs = ["statespace.h"],
deps = [":util"],
)

cc_library(
Expand Down
36 changes: 36 additions & 0 deletions lib/circuit_qsim_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,21 @@ class CircuitQsimParser final {
ss >> q0 >> q1 >> phi;
if (!ValidateGate(ss, num_qubits, q0, q1, provider, k)) return false;
gates.push_back(GateCP<fp_type>::Create(time, q0, q1, phi));
} else if (gate_name == "m") {
std::vector<unsigned> qubits;
qubits.reserve(num_qubits);
do {
ss >> q0;
if (ss) {
qubits.push_back(q0);
} else {
InvalidGateError(provider, k);
return false;
}
} while (ss.peek() != std::stringstream::traits_type::eof());
if (!ValidateGate(ss, num_qubits, qubits, provider, k)) return false;
gates.push_back(gate::Measurement<GateQSim<fp_type>>::Create(
time, std::move(qubits)));
} else {
InvalidGateError(provider, k);
return false;
Expand Down Expand Up @@ -247,6 +262,27 @@ class CircuitQsimParser final {

return true;
}

/**
* Checks formatting for a multiqubit gate parsed from 'ss'.
* @param ss Input stream containing the gate specification.
* @param num_qubits Number of qubits, as defined at the start of the file.
* @param qubits Indices of affected qubits.
* @param provider Circuit source; only used for error reporting.
* @param line Line number of the parsed gate; only used for error reporting.
*/
static bool ValidateGate(std::stringstream& ss, unsigned num_qubits,
const std::vector<unsigned>& qubits,
const std::string& provider, unsigned line) {
for (auto q : qubits) {
if (q >= num_qubits) {
InvalidGateError(provider, line);
return false;
}
}

return true;
}
};

} // namespace qsim
Expand Down
2 changes: 1 addition & 1 deletion lib/fuser.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct GateFused {
typename Gate::GateKind kind; // Kind of the (first) master gate.
unsigned time;
unsigned num_qubits;
unsigned qubits[3];
std::vector<unsigned> qubits;
const Gate* pmaster;
std::vector<const Gate*> gates;
};
Expand Down
Loading

0 comments on commit 8ad553c

Please sign in to comment.