Skip to content

Commit

Permalink
Add w state implementation (#391)
Browse files Browse the repository at this point in the history
## Description

Implement WState from
[this](https://github.com/cda-tum/mqt-bench/blob/main/src/mqt/bench/benchmarks/wstate.py)
`mqt-bench` script in light of the DD benchmarking in [this
PR](tyi1025/dd_eval#12).

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Lukas Burgholzer <[email protected]>
  • Loading branch information
3 people authored Aug 9, 2023
1 parent bd41474 commit da83c0b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
10 changes: 10 additions & 0 deletions include/algorithms/WState.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <QuantumComputation.hpp>

namespace qc {
class WState : public QuantumComputation {
public:
explicit WState(Qubit nq);
};
} // namespace qc
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if(NOT TARGET ${PROJECT_NAME})
${PROJECT_SOURCE_DIR}/include/algorithms/QFT.hpp
${PROJECT_SOURCE_DIR}/include/algorithms/QPE.hpp
${PROJECT_SOURCE_DIR}/include/algorithms/RandomCliffordCircuit.hpp
${PROJECT_SOURCE_DIR}/include/algorithms/WState.hpp
${PROJECT_SOURCE_DIR}/include/CircuitOptimizer.hpp
${PROJECT_SOURCE_DIR}/include/Definitions.hpp
${PROJECT_SOURCE_DIR}/include/operations/Expression.hpp
Expand All @@ -50,6 +51,7 @@ if(NOT TARGET ${PROJECT_NAME})
algorithms/QFT.cpp
algorithms/QPE.cpp
algorithms/RandomCliffordCircuit.cpp
algorithms/WState.cpp
CircuitOptimizer.cpp
operations/Expression.cpp
operations/NonUnitaryOperation.cpp
Expand Down
25 changes: 25 additions & 0 deletions src/algorithms/WState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "algorithms/WState.hpp"

namespace qc {
void fGate(QuantumComputation& qc, const Qubit i, const Qubit j, const Qubit k,
const Qubit n) {
const auto theta = std::acos(std::sqrt(1.0 / static_cast<double>(k - n + 1)));
qc.ry(j, -theta);
qc.z(j, qc::Control{i});
qc.ry(j, theta);
}

WState::WState(const Qubit nq) : QuantumComputation(nq) {
name = "wstate_" + std::to_string(nq);

x(nq - 1);

for (Qubit m = 1; m < nq; m++) {
fGate(*this, nq - m, nq - m - 1, nq, m);
}

for (Qubit k = nq - 1; k > 0; k--) {
x(k, qc::Control{k - 1});
}
}
} // namespace qc
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ package_add_test(
algorithms/test_entanglement.cpp
algorithms/test_grcs.cpp
algorithms/test_random_clifford.cpp
algorithms/test_qpe.cpp)
algorithms/test_qpe.cpp
algorithms/test_wstate.cpp)

package_add_test(
${PROJECT_NAME}-test-zx
Expand Down
42 changes: 42 additions & 0 deletions test/algorithms/test_wstate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "algorithms/WState.hpp"
#include "dd/Simulation.hpp"

#include "gtest/gtest.h"
#include <iostream>
#include <vector>

class WState : public testing::TestWithParam<qc::Qubit> {};

std::vector<std::string> generateWStateStrings(const std::size_t length) {
std::vector<std::string> result;
result.reserve(length);
for (std::size_t i = 0U; i < length; ++i) {
auto binaryString = std::string(length, '0');
binaryString[i] = '1';
result.emplace_back(binaryString);
}
return result;
}

INSTANTIATE_TEST_SUITE_P(
WState, WState, testing::Range<qc::Qubit>(1U, 128U, 7U),
[](const testing::TestParamInfo<WState::ParamType>& inf) {
// Generate names for test cases
const auto nqubits = inf.param;
std::stringstream ss{};
ss << nqubits << "_qubits";
return ss.str();
});

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

auto qc = qc::WState(nq);
auto dd = std::make_unique<dd::Package<>>(qc.getNqubits());
const std::size_t shots = 1024;
const auto measurements =
simulate(&qc, dd->makeZeroState(qc.getNqubits()), dd, shots);
for (const auto& result : generateWStateStrings(nq)) {
EXPECT_TRUE(measurements.find(result) != measurements.end());
}
}

1 comment on commit da83c0b

@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.