-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## 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
1 parent
bd41474
commit da83c0b
Showing
5 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
da83c0b
There was a problem hiding this comment.
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.