Skip to content

Commit

Permalink
Upgrade MQT Core (#263)
Browse files Browse the repository at this point in the history
* Upgrade MQT Core

- Includes improved memory management
- Adapt the simulator code
- Added Tests

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
hillmich and pre-commit-ci[bot] authored Jul 18, 2023
1 parent 0590832 commit 156bd70
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.idea
venv/
.venv/
.vscode/
*.egg-info/
__pycache__/
*.so
Expand Down
3 changes: 2 additions & 1 deletion apps/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ int main(int argc, char** argv) { // NOLINT(bugprone-exception-escape)
}

if (vm.count("pcomplex") > 0) {
outputObj["complex_stats"] = ddsim->dd->cn.complexTable.getStatistics();
outputObj["complex_stats"]["cache_count"] = ddsim->dd->cn.cacheCount();
outputObj["complex_stats"]["real_count"] = ddsim->dd->cn.realCount();
}

if (vm.count("dump_complex") > 0) {
Expand Down
4 changes: 2 additions & 2 deletions include/DeterministicNoiseSimulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class DeterministicNoiseSimulator: public Simulator<Config> {

[[nodiscard]] std::string getName() const override { return qc->getName(); };

[[nodiscard]] std::size_t getActiveNodeCount() const override { return Simulator<Config>::dd->dUniqueTable.getActiveNodeCount(); }
[[nodiscard]] std::size_t getMaxNodeCount() const override { return Simulator<Config>::dd->dUniqueTable.getMaxActiveNodes(); }
[[nodiscard]] std::size_t getActiveNodeCount() const override { return Simulator<Config>::dd->template getUniqueTable<dd::dNode>().getStats().activeEntryCount; }
[[nodiscard]] std::size_t getMaxNodeCount() const override { return Simulator<Config>::dd->template getUniqueTable<dd::dNode>().getStats().peakActiveEntryCount; }

[[nodiscard]] std::size_t countNodesFromRoot() override {
if (useDensityMatrixType) {
Expand Down
10 changes: 5 additions & 5 deletions include/Simulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ class Simulator {
return results;
}

[[nodiscard]] virtual std::size_t getActiveNodeCount() const { return dd->vUniqueTable.getActiveNodeCount(); }
[[nodiscard]] virtual std::size_t getActiveNodeCount() const { return dd->template getUniqueTable<dd::vNode>().getStats().activeEntryCount; }

[[nodiscard]] virtual std::size_t getMaxNodeCount() const { return dd->vUniqueTable.getMaxActiveNodes(); }
[[nodiscard]] virtual std::size_t getMaxNodeCount() const { return dd->template getUniqueTable<dd::vNode>().getStats().peakActiveEntryCount; }

[[nodiscard]] virtual std::size_t getMaxMatrixNodeCount() const { return dd->mUniqueTable.getMaxActiveNodes(); }
[[nodiscard]] virtual std::size_t getMaxMatrixNodeCount() const { return dd->template getUniqueTable<dd::mNode>().getStats().activeEntryCount; }

[[nodiscard]] virtual std::size_t getMatrixActiveNodeCount() const { return dd->mUniqueTable.getActiveNodeCount(); }
[[nodiscard]] virtual std::size_t getMatrixActiveNodeCount() const { return dd->template getUniqueTable<dd::mNode>().getStats().activeEntryCount; }

[[nodiscard]] virtual std::size_t countNodesFromRoot() { return dd->size(rootEdge); }

Expand Down Expand Up @@ -118,7 +118,7 @@ class Simulator {
dd->cn.setTolerance(tolerance);
}
[[nodiscard]] dd::fp getTolerance() const {
return dd->cn.complexTable.tolerance();
return dd::RealNumber::eps;
}

[[nodiscard]] std::vector<std::priority_queue<std::pair<double, dd::vNode*>, std::vector<std::pair<double, dd::vNode*>>>> getNodeContributions(const dd::vEdge& edge) const;
Expand Down
2 changes: 1 addition & 1 deletion include/UnitarySimulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class UnitarySimulator: public CircuitSimulator<Config> {
[[nodiscard]] qc::MatrixDD getConstructedDD() const { return e; }
[[nodiscard]] double getConstructionTime() const { return constructionTime; }
[[nodiscard]] std::size_t getFinalNodeCount() const { return Simulator<Config>::dd->size(e); }
[[nodiscard]] std::size_t getMaxNodeCount() const override { return Simulator<Config>::dd->mUniqueTable.getPeakNodeCount(); }
[[nodiscard]] std::size_t getMaxNodeCount() const override { return Simulator<Config>::dd->template getUniqueTable<dd::mNode>().getStats().peakActiveEntryCount; }

private:
qc::MatrixDD e{};
Expand Down
2 changes: 1 addition & 1 deletion mqt/ddsim/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ std::unique_ptr<Simulator> constructSimulatorWithoutSeed(const py::object& circ,

void getNumpyMatrixRec(const qc::MatrixDD& e, const std::complex<dd::fp>& amp, std::size_t i, std::size_t j, std::size_t dim, std::complex<dd::fp>* mat) {
// calculate new accumulated amplitude
auto w = std::complex<dd::fp>{dd::CTEntry::val(e.w.r), dd::CTEntry::val(e.w.i)};
auto w = std::complex<dd::fp>{dd::RealNumber::val(e.w.r), dd::RealNumber::val(e.w.i)};
auto c = amp * w;

// base case
Expand Down
8 changes: 4 additions & 4 deletions src/Simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ double Simulator<Config>::approximateByFidelity(std::unique_ptr<dd::Package<Conf
}

dd::vEdge newEdge = removeNodes(localDD, edge, dagEdges);
assert(!std::isnan(dd::CTEntry::val(edge.w.r)));
assert(!std::isnan(dd::CTEntry::val(edge.w.i)));
assert(!std::isnan(dd::RealNumber::val(edge.w.r)));
assert(!std::isnan(dd::RealNumber::val(edge.w.i)));
dd::Complex c = localDD->cn.getCached(std::sqrt(CN::mag2(newEdge.w)), 0);
CN::div(c, newEdge.w, c);
newEdge.w = localDD->cn.lookup(c);
Expand Down Expand Up @@ -325,7 +325,7 @@ std::pair<dd::ComplexValue, std::string> Simulator<Config>::getPathOfLeastResist
}

std::string result(getNumberOfQubits(), '0');
dd::Complex pathValue = dd->cn.getCached(dd::CTEntry::val(rootEdge.w.r), dd::CTEntry::val(rootEdge.w.i));
dd::Complex pathValue = dd->cn.getCached(dd::RealNumber::val(rootEdge.w.r), dd::RealNumber::val(rootEdge.w.i));
dd::vEdge cur = rootEdge;
for (dd::Qubit i = rootEdge.p->v; i >= 0; --i) {
dd::fp p0 = dd::ComplexNumbers::mag2(cur.p->e.at(0).w);
Expand All @@ -347,7 +347,7 @@ std::pair<dd::ComplexValue, std::string> Simulator<Config>::getPathOfLeastResist
}
}

return {{dd::CTEntry::val(pathValue.r), dd::CTEntry::val(pathValue.i)},
return {{dd::RealNumber::val(pathValue.r), dd::RealNumber::val(pathValue.i)},
std::string{result.rbegin(), result.rend()}};
}

Expand Down
12 changes: 4 additions & 8 deletions test/test_circuit_sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,16 @@ TEST(CircuitSimTest, GRCS4x4Test) {
CircuitSimulator ddsim(std::make_unique<qc::QuantumComputation>("circuits/inst_4x4_10_0.txt"));
auto m = ddsim.simulate(100);
EXPECT_GT(m.size(), 0);
ddsim.dd->cn.complexTable.printStatistics();
}
{
CircuitSimulator ddsim(std::make_unique<qc::QuantumComputation>("circuits/inst_4x4_10_1.txt"));
auto m = ddsim.simulate(100);
EXPECT_GT(m.size(), 0);
ddsim.dd->cn.complexTable.printStatistics();
}
{
CircuitSimulator ddsim(std::make_unique<qc::QuantumComputation>("circuits/inst_4x4_10_2.txt"));
auto m = ddsim.simulate(100);
EXPECT_GT(m.size(), 0);
ddsim.dd->cn.complexTable.printStatistics();
}
}

Expand All @@ -243,6 +240,8 @@ TEST(CircuitSimTest, TestingProperties) {
EXPECT_EQ(ddsim.getMaxMatrixNodeCount(), 0);
EXPECT_EQ(ddsim.getMatrixActiveNodeCount(), 0);
EXPECT_EQ(ddsim.countNodesFromRoot(), 7);
EXPECT_EQ(ddsim.getSeed(), "1");
EXPECT_EQ(ddsim.additionalStatistics().at("approximation_runs"), "0");
}

TEST(CircuitSimTest, ApproximationTest) {
Expand Down Expand Up @@ -301,15 +300,12 @@ TEST(CircuitSimTest, ToleranceTest) {
// A small test to make sure that setting and getting the tolerance works
auto qc = std::make_unique<qc::QuantumComputation>(2);
CircuitSimulator ddsim(std::move(qc));
const auto tolerance = ddsim.getTolerance();
EXPECT_EQ(tolerance, dd::ComplexTable::tolerance());
const auto newTolerance = 0.1;
const auto tolerance = ddsim.getTolerance();
const auto newTolerance = 0.1;
ddsim.setTolerance(newTolerance);
EXPECT_EQ(ddsim.getTolerance(), newTolerance);
EXPECT_EQ(dd::ComplexTable::tolerance(), newTolerance);
ddsim.setTolerance(tolerance);
EXPECT_EQ(ddsim.getTolerance(), tolerance);
EXPECT_EQ(dd::ComplexTable::tolerance(), tolerance);
}

TEST(CircuitSimTest, TooManyQubitsForVectorTest) {
Expand Down

1 comment on commit 156bd70

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ✔️

No problems need attention.

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.