Skip to content

Commit cb645b6

Browse files
authored
Merge pull request #3 from sam-jaques/dmitryv/sparse-simulator-feedback-3
Unit tests, comments and feedback from review
2 parents 3375a20 + 5667211 commit cb645b6

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/Simulation/Simulators/SparseSimulator/Native/SparseSimulator.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,18 @@ std::shared_ptr<BasicQuantumState> expand_wfn_helper(std::shared_ptr<BasicQuantu
4747
return (nqubits > max_num_bits / 2) ? std::shared_ptr<BasicQuantumState>(new QuantumState<max_num_bits>(old_sim)): expand_wfn_helper<max_num_bits / 2>(old_sim, nqubits);
4848
}
4949

50+
// Sparse simulator only stores non-zero coefficients of the quantum state.
51+
// It has good performance only when the number of non-zero coefficients is low.
52+
// If the number of non-zero coefficients is low, the number of qubits may be fairly large.
53+
// Sparse simulator employs hashtable structure (by Malte Skarupke) to store non-zero coefficients.
54+
// Keys are basis vectors represented by std::bitset<>.
55+
// Values are non-zero amplitudes represented by std::complex<RealType>.
56+
// Zero amplitudes are simply not stored.
57+
// Hashtable is reallocated and reconstructed on almost every gate.
58+
// Reallocation is saved for some gates that can be performed in one round.
5059
class SparseSimulator
5160
{
52-
public:
61+
public:
5362

5463
std::set<std::string> operations_done;
5564

@@ -584,13 +593,16 @@ class SparseSimulator
584593
// as a phase/permutation gate
585594
// This means if an assert fails, it will fail
586595
// at some future point, not at the point of failure
587-
#if NDEBUG
596+
#if defined(NDEBUG)
588597
if (isAllZ) {
589598
_queued_operations.push_back(operation(OP::Assert, qubits, result));
599+
// In release mode we queue Z assertion and return.
590600
return;
591601
}
592602
#endif
593-
// X or Y assertions require execution
603+
// X or Y assertions require immediate execution.
604+
// We also execute Z assertion immediately in debug mode
605+
// to provide feedback at the point of failure.
594606
_execute_queued_ops(qubits, OP::PermuteLarge);
595607
_quantum_state->Assert(axes, qubits, result);
596608
}

src/Simulation/Simulators/SparseSimulator/SparseSimulatorTests/SparseSimulatorTests.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
#include "TestHelpers.hpp"
88
#include <cmath>
99
#include <iostream>
10-
#include <locale> // necessary for string conversions for logging
11-
#include <codecvt> // necessary for string conversions for logging
12-
#include <string> // necessary for string conversions for logging
1310

1411
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
1512
using namespace Microsoft::Quantum::SPARSESIMULATOR;
@@ -70,15 +67,17 @@ namespace SparseSimulatorTests
7067
uint64_t k = 0;
7168
qubit_label_type<num_qubits> label1(0);
7269
qubit_label_type<num_qubits> label2(1);
73-
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
70+
7471
for (i = 0; i < 500; i++){
7572
k += i * i * i * i;
7673
uint64_t m = 0;
7774
label1 = qubit_label_type<num_qubits>(k);
7875
for (j = 0; j < 500; j++){
7976
m += j * j * j * j;
8077
label2 = qubit_label_type<num_qubits>(m);
81-
Assert::AreEqual<bool>(k < m, label1 < label2, converter.from_bytes("Comparing " + std::to_string(k) + " to " + std::to_string(m) + "\n").c_str());
78+
wchar_t message[100];
79+
swprintf(message, sizeof(message)/sizeof(*message), L"Comparing %llu to %llu\n", k, m);
80+
Assert::AreEqual<bool>(k < m, label1 < label2, message);
8281
}
8382
}
8483
}

0 commit comments

Comments
 (0)