Skip to content

Commit

Permalink
[feat]: add naive final measure
Browse files Browse the repository at this point in the history
  • Loading branch information
lucky9-cyou committed Dec 21, 2023
1 parent 2e4d8e0 commit ac794a5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/qfvm/qfvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,9 @@ template <size_t word_size>
std::map<uint, uint> simulate_circuit_clifford(const py::object& pycircuit, const int& shots) {

auto circuit = Circuit(pycircuit);
auto global_cs = circuit_simulator<word_size>(circuit.qubit_num());

// If measure all at the end, simulate once
uint actual_shots = shots;
if (circuit.final_measure())
actual_shots = 1;

// qbit, cbit
vector<std::pair<uint, uint>> measures = circuit.measure_vec();
Expand All @@ -136,10 +133,11 @@ std::map<uint, uint> simulate_circuit_clifford(const py::object& pycircuit, cons
// Store outcome's count
std::map<uint, uint> outcount;

for(uint i =0; i < actual_shots; i++) {
circuit_simulator<word_size> cs(circuit.qubit_num());

circuit_simulator<word_size> cs(circuit.qubit_num());
for(uint i =0; i < actual_shots; i++) {
simulate(circuit, cs);
uint outcome = 0;

if (!circuit.final_measure()) {
// qubit, cbit, measure result
Expand All @@ -149,24 +147,34 @@ std::map<uint, uint> simulate_circuit_clifford(const py::object& pycircuit, cons
std::sort(measure_results.begin(), measure_results.end(),
[](auto& a, auto& b) { return std::get<1>(a) < std::get<1>(b); });

uint outcome = 0;
for (auto& measure_result: measure_results) {
outcome *= 2;
outcome += std::get<2>(measure_result);
}

if (outcount.find(outcome) != outcount.end())
outcount[outcome]++;
else
outcount[outcome] = 1;
} else if (circuit.final_measure() && !measures.empty()) {
for (auto& measure: measures) {
cs.do_circuit_instruction({"measure", std::vector<size_t>{measures[i].first}, std::vector<double>{static_cast<double>(measures[i].second)}});
}

// qubit, cbit, measure result
auto measure_results = cs.current_measurement_record();

// make sure the order is the same with other simulators
std::sort(measure_results.begin(), measure_results.end(),
[](auto& a, auto& b) { return std::get<1>(a) < std::get<1>(b); });

for (auto& measure_result: measure_results) {
outcome *= 2;
outcome += std::get<2>(measure_result);
}
}

if (circuit.final_measure() || i == actual_shots - 1)
global_cs = std::move(cs);
}
if(outcount.find(outcome) != outcount.end()) outcount[outcome]++;
else outcount[outcome] = 1;

if (circuit.final_measure()) {
throw std::runtime_error("Not implemented");
cs.reset_tableau();
cs.sim_record.clear();
}

return outcount;
Expand Down
11 changes: 11 additions & 0 deletions src/qfvm_clifford/clifford_simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct measurement_record {
auto cbegin() const { return storage.cbegin(); }
auto cend() const { return storage.cend(); }
auto size() const { return storage.size(); }
void clear() { storage.clear(); }
};

// quantum circuit simulator, include original tableau, measurement record and a
Expand Down Expand Up @@ -155,6 +156,16 @@ template <size_t word_size> struct circuit_simulator {

// get the measurement record
auto current_measurement_record() const { return sim_record; }

auto measure_all() {
measurement_record mr;
for (size_t i = 0; i < sim_tableau.num_qubits; i++) {
auto record = sim_tableau.m_gate(rng, i);
if (record.has_value())
mr.record(record.value());
}
return mr;
}
};

#endif

0 comments on commit ac794a5

Please sign in to comment.