Skip to content

✨ added state preparation routines for GHZ and W state #445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9c3eb8d
:sparkles: added state preparation routines for GHZ and W state
BertiFlorea Oct 19, 2023
f6b40ec
:adhesive_bandage: fixed GHZ state preparation routine
BertiFlorea Oct 23, 2023
cf57c39
🎨 pre-commit fixes
pre-commit-ci[bot] Oct 23, 2023
75d5359
:adhesive_bandage: fixed W state preparation routine
BertiFlorea Oct 24, 2023
f78359d
Merge branch 'add-state-preparation-routines' of github.com:BertiFlor…
BertiFlorea Oct 24, 2023
46f26bd
Merge branch 'main' into add-state-preparation-routines
BertiFlorea Oct 24, 2023
97a22ad
:twisted_rightwards_arrows: merged updates from cda-tum/mqt-core:main
BertiFlorea Oct 24, 2023
748c05a
:recycle: moved state preparation test to a different source file and…
BertiFlorea Oct 24, 2023
efd0c6c
🎨 pre-commit fixes
pre-commit-ci[bot] Oct 24, 2023
efb3904
:art: format code
BertiFlorea Oct 24, 2023
49b0ca0
Merge branch 'add-state-preparation-routines' of github.com:BertiFlor…
BertiFlorea Oct 24, 2023
10f436c
Merge branch 'main' into add-state-preparation-routines
burgholzer Oct 24, 2023
5a26e64
Update include/dd/Package.hpp
BertiFlorea Oct 27, 2023
f0aa065
Update include/dd/Package.hpp
BertiFlorea Oct 27, 2023
34cca53
🎨 pre-commit fixes
pre-commit-ci[bot] Oct 27, 2023
41ddc40
Update include/dd/Package.hpp
BertiFlorea Oct 27, 2023
cfa6fab
Update include/dd/Package.hpp
BertiFlorea Oct 27, 2023
a72db71
🎨 pre-commit fixes
pre-commit-ci[bot] Oct 27, 2023
22bb904
:white_check_mark: fixed edge cases tests and updated W state prepara…
BertiFlorea Oct 29, 2023
12d4a02
Merge remote-tracking branch 'upstream/main' into add-state-preparati…
BertiFlorea Oct 29, 2023
f37e422
:bug: fix weight update (setVal function no longer exists)
BertiFlorea Oct 29, 2023
cad5dcc
⚡🚧 optimize W-State construction
burgholzer Oct 31, 2023
abfce06
:white_check_mark: updated test cases for the state preparation routi…
BertiFlorea Nov 14, 2023
ac1ace9
Merge remote-tracking branch 'upstream/main' into add-state-preparati…
BertiFlorea Nov 14, 2023
818983d
:rewind: reverted git submodules
BertiFlorea Nov 15, 2023
ae4679c
:fire: added requested changes
BertiFlorea Nov 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions include/dd/Package.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,74 @@ template <class Config> class Package {
return f;
}

// generate general GHZ state with n qubits
vEdge makeGHZState(const std::size_t n) {
if (n > nqubits) {
throw std::runtime_error{
"Requested state with " + std::to_string(n) +
" qubits, but current package configuration only supports up to " +
std::to_string(nqubits) +
" qubits. Please allocate a larger package instance."};
}

if (n == 0U) {
return vEdge::one();
}

auto leftSubtree = vEdge::one();
auto rightSubtree = vEdge::one();

for (std::size_t p = 0; p < n - 1; ++p) {
leftSubtree = makeDDNode(static_cast<Qubit>(p),
std::array{leftSubtree, vEdge::zero()});
rightSubtree = makeDDNode(static_cast<Qubit>(p),
std::array{vEdge::zero(), rightSubtree});
}

return makeDDNode(
static_cast<Qubit>(n - 1),
std::array<vEdge, RADIX>{
{{leftSubtree.p, {&constants::sqrt2over2, &constants::zero}},
{rightSubtree.p, {&constants::sqrt2over2, &constants::zero}}}});
}

// generate general W state with n qubits
vEdge makeWState(const std::size_t n) {
if (n > nqubits) {
throw std::runtime_error{
"Requested state with " + std::to_string(n) +
" qubits, but current package configuration only supports up to " +
std::to_string(nqubits) +
" qubits. Please allocate a larger package instance."};
}

if (n == 0U) {
return vEdge::one();
}

auto leftSubtree = vEdge::zero();
if ((1. / sqrt(static_cast<double>(n))) < RealNumber::eps) {
throw std::runtime_error(
"Requested qubit size for generating W-state would lead to an "
"underflow due to 1 / sqrt(n) being smaller than the currently set "
"tolerance " +
std::to_string(RealNumber::eps) +
". If you still wanna run the computation, please lower "
"the tolerance accordingly.");
}

auto rightSubtree = vEdge::terminal(cn.lookup(1. / std::sqrt(n)));
for (size_t p = 0; p < n; ++p) {
leftSubtree = makeDDNode(static_cast<Qubit>(p),
std::array{leftSubtree, rightSubtree});
if (p != n - 1U) {
rightSubtree = makeDDNode(static_cast<Qubit>(p),
std::array{rightSubtree, vEdge::zero()});
}
}
return leftSubtree;
}

// generate the decision diagram from an arbitrary state vector
vEdge makeStateFromVector(const CVec& stateVector) {
if (stateVector.empty()) {
Expand Down
21 changes: 21 additions & 0 deletions test/algorithms/test_entanglement.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "algorithms/Entanglement.hpp"
#include "dd/FunctionalityConstruction.hpp"
#include "dd/Simulation.hpp"

#include "gtest/gtest.h"
#include <string>
Expand Down Expand Up @@ -33,3 +34,23 @@ TEST_P(Entanglement, FunctionTest) {
ASSERT_EQ(r.getValueByPath(std::string(nq, '0')), dd::SQRT2_2);
ASSERT_EQ(r.getValueByPath(std::string(nq, '1')), dd::SQRT2_2);
}

TEST_P(Entanglement, GHZRoutineFunctionTest) {
const auto nq = GetParam();

auto qc = qc::Entanglement(nq);
auto dd = std::make_unique<dd::Package<>>(nq);
const dd::VectorDD e = simulate(&qc, dd->makeZeroState(qc.getNqubits()), dd);
const auto f = dd->makeGHZState(nq);

EXPECT_EQ(e, f);
}

TEST(Entanglement, GHZStateEdgeCasesTest) {
auto dd = std::make_unique<dd::Package<>>(3);

EXPECT_EQ(dd->makeGHZState(0),
dd->makeBasisState(0, {dd::BasisStates::zero}));
EXPECT_EQ(dd->makeGHZState(0), dd->makeBasisState(0, {dd::BasisStates::one}));
ASSERT_THROW(dd->makeGHZState(6), std::runtime_error);
}
22 changes: 22 additions & 0 deletions test/algorithms/test_wstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,25 @@ TEST_P(WState, FunctionTest) {
EXPECT_TRUE(measurements.find(result) != measurements.end());
}
}

TEST_P(WState, RoutineFunctionTest) {
const auto nq = GetParam();

auto qc = qc::WState(nq);
auto dd = std::make_unique<dd::Package<>>(qc.getNqubits());
const dd::VectorDD e = simulate(&qc, dd->makeZeroState(qc.getNqubits()), dd);
const auto f = dd->makeWState(nq);

EXPECT_EQ(e, f);
}

TEST(WState, WStateEdgeCasesTest) {
auto dd = std::make_unique<dd::Package<>>(101);
dd::ComplexNumbers::setTolerance(0.1);

ASSERT_THROW(dd->makeWState(101), std::runtime_error);
EXPECT_EQ(dd->makeWState(0), dd->makeBasisState(0, {dd::BasisStates::zero}));
EXPECT_EQ(dd->makeWState(0), dd->makeBasisState(0, {dd::BasisStates::one}));
EXPECT_EQ(dd->makeWState(1), dd->makeBasisState(1, {dd::BasisStates::one}));
ASSERT_THROW(dd->makeWState(127), std::runtime_error);
}