Skip to content

Commit

Permalink
Document result distribution and implement as part of qsim app
Browse files Browse the repository at this point in the history
  • Loading branch information
sethrj committed Feb 26, 2025
1 parent 54a0b45 commit 9a1a019
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 50 deletions.
49 changes: 7 additions & 42 deletions app/qir-qsim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
#include <cstdlib>
#include <iostream>
#include <string>
#include <string_view>
#include <CLI/CLI.hpp>

#include "qiree_version.h"

#include "qiree/Executor.hh"
#include "qiree/Module.hh"
#include "qiree/QuantumNotImpl.hh"
#include "qiree/ResultDistribution.hh"
#include "qirqsim/QsimDefaultRuntime.hh"
#include "qirqsim/QsimQuantum.hh"

Expand All @@ -25,50 +22,25 @@ namespace qiree
{
namespace app
{
//---------------------------------------------------------------------------//
void run(std::string const& filename, int num_shots)
// bool group_tuples = false)
{
// Load the input
Executor execute{Module{filename}};

// Set up qsim
QsimQuantum sim(std::cout, 0);

// Collect the statistics
std::unique_ptr<RuntimeInterface> rt;
rt = std::make_unique<QsimDefaultRuntime>(std::cout, sim);
QsimDefaultRuntime rt(std::cout, sim);
ResultDistribution distribution;

// Run several time = shots (default 1)
for (int i = 0; i < num_shots; i++)
{
execute(sim, *rt);
execute(sim, rt);
distribution.accumulate(rt.result());
}

std::cout << std::endl;
std::cout << "Measurement output:" << std::endl;
std::cout << "-------------------" << std::endl;
std::cout << "Number of shots: " << num_shots << std::endl;
std::cout << "Number of qubits: " << sim.num_qubits() << std::endl;

for (int q_index = 0; q_index < sim.num_qubits(); q_index++)
{
int value_0 = 0;
int value_1 = 0;
if (auto value
= sim.manager.getBufferValue("q" + std::to_string(q_index), "0");
value.has_value())
{
value_0 = value.value();
}
if (auto value
= sim.manager.getBufferValue("q" + std::to_string(q_index), "1");
value.has_value())
{
value_1 = value.value();
}
std::cout << "q" << q_index << " {0: " << value_0 << ","
<< " 1: " << value_1 << "}\n";
}
std::cout << distribution.to_json() << std::endl;
}

//---------------------------------------------------------------------------//
Expand All @@ -83,7 +55,6 @@ int main(int argc, char* argv[])
{
int num_shots{1};
std::string filename;
// bool group_tuples{false};

CLI::App app;

Expand All @@ -95,14 +66,8 @@ int main(int argc, char* argv[])
= app.add_option("-s,--shots", num_shots, "Number of shots");
nshot_opt->capture_default_str();

// app.add_flag("--group-tuples,!--no-group-tuples",
// group_tuples,
// "Print per-tuple measurement statistics rather than "
// "per-qubit");

CLI11_PARSE(app, argc, argv);

// qiree::app::run(filename, num_shots, group_tuples);
qiree::app::run(filename, num_shots);

return EXIT_SUCCESS;
Expand Down
19 changes: 11 additions & 8 deletions src/qiree/ResultDistribution.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ class RecordedResult;
//---------------------------------------------------------------------------//
/*!
* Distribution of recorded results from shots.
The distribution is formed by accumulating multiple \c RecordedResult
instances into a sparse map of {bit string -> count}. The length of the bit
string is the same for all keys. We provide accessors by string key such
as \c 01001 which corresponds to a bit string `{false, true, ...}`. It is
serializable to JSON, creating an object that has bit strings as keys and
counts as values.
* The distribution is formed by accumulating multiple \c RecordedResult
* instances into a sparse map of {bit string -> count}. The length of the bit
* string is the same for all keys. We provide accessors by little-endian
* string key such as \c "01001" which corresponds to a bit string \code
* {false, true, false, false, true}
* \endcode
* Little-endian is an "array-like" representation (the initial
* character corresponds to the zeroth qubit) rather than a "number-like"
* representation (i.e., a base-2 encoding of the qubits). It is serializable
* to JSON, creating an object that has bit strings as keys and counts as
* values.
*/
class ResultDistribution
{
Expand Down

0 comments on commit 9a1a019

Please sign in to comment.