Skip to content
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

Add w state implementation #391

Merged
merged 17 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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());
}
}