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

Fixes #975 : Implement QFT in lightning.qubit and benchmark #990

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions pennylane_lightning/core/src/gates/GateOperation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ enum class GateOperation : uint32_t {
/* Multi-qubit gates */
MultiRZ,
GlobalPhase,
/* Quantum Algorithm */
QFT,
/* END (placeholder) */
END
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ using Pennylane::Gates::GateOperation;
/// @endcond

namespace Pennylane::LightningQubit::Gates {

/**
* @brief Return a specific member function pointer for a given gate operation.
* See specialized classes.
Expand Down Expand Up @@ -265,6 +266,12 @@ struct GateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
constexpr static auto value =
&GateImplementation::template applyGlobalPhase<PrecisionT, ParamT>;
};
template <class PrecisionT, class ParamT, class GateImplementation>
struct GateOpToMemberFuncPtr<PrecisionT, ParamT, GateImplementation,
GateOperation::QFT> {
constexpr static auto value =
&GateImplementation::template applyQFT<PrecisionT>;
};

template <class PrecisionT, class ParamT, class GateImplementation,
ControlledGateOperation gate_op>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,24 @@ class GateImplementationsLM : public PauliGenerator<GateImplementationsLM> {
return -static_cast<PrecisionT>(0.5);
}

template <class PrecisionT>
[[nodiscard]] static auto applyQFT(
std::complex<PrecisionT> *arr, std::size_t num_qubits,
const std::vector<std::size_t> &wires, bool inverse = false) {
auto local_wires = wires;
for (std::size_t i = 0; i < local_wires.size(); ++i) {
applyHadamard(arr, num_qubits, {local_wires[i]}, inverse);
for (std::size_t j = i + 1; j < local_wires.size(); ++j) {
PrecisionT angle = M_PI / (1U << (j - i));
if (inverse) {
angle = -angle;
}
applyControlledPhaseShift(arr, num_qubits, {local_wires[j], local_wires[i]}, inverse, angle);
}
}
std::reverse(local_wires.begin(), local_wires.end());
}

template <class PrecisionT>
[[nodiscard]] static auto
applyNCGeneratorMultiRZ(std::complex<PrecisionT> *arr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,24 @@ template <typename PrecisionT, class GateImplementation> void testApplyCSWAP() {
}
PENNYLANE_RUN_TEST(CSWAP);

template <typename PrecisionT, class GateImplementation> void testApplyQFT() {
using ComplexT = std::complex<PrecisionT>;
const std::size_t num_qubits = 3;

auto st = createZeroState<ComplexT>(num_qubits);
GateImplementation::applyQFT(st.data(), num_qubits, {0, 1, 2}, false);

// Expected result of QFT(|000>) for a 3-qubit system
std::vector<ComplexT> expected_result = {
ComplexT{0.353553, 0.0}, ComplexT{0.353553, 0.0}, ComplexT{0.353553, 0.0},
ComplexT{0.353553, 0.0}, ComplexT{0.353553, 0.0}, ComplexT{0.353553, 0.0},
ComplexT{0.353553, 0.0}, ComplexT{0.353553, 0.0}
};

CHECK(st == approx(expected_result).margin(1e-7));
}
PENNYLANE_RUN_TEST(QFT);

TEMPLATE_TEST_CASE("StateVectorLQubitManaged::applyOperation non-param "
"one-qubit with controls",
"[StateVectorLQubitManaged]", float, double) {
Expand Down
1 change: 1 addition & 0 deletions pennylane_lightning/lightning_qubit/lightning_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"ECR",
"BlockEncode",
"C(BlockEncode)",
"QFT",
}
)
# End the set of supported operations.
Expand Down
15 changes: 15 additions & 0 deletions tests/test_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,18 @@ def circuit():
circ = qml.QNode(circuit, dev)
circ_def = qml.QNode(circuit, dev_def)
assert np.allclose(circ(), circ_def(), tol)

@pytest.mark.parametrize("num_qubits", [2, 3, 4])
def test_qft_gate(num_qubits):
dev = qml.device("default.qubit", wires=num_qubits)
@qml.qnode(dev)
def circuit():
qml.QFT(wires=range(num_qubits))
return qml.state()
output_state = circuit()
expected_matrix = qml.QFT.compute_matrix(num_qubits)
initial_state = np.zeros((2**num_qubits,), dtype=np.complex128)
initial_state[0] = 1
expected_state = expected_matrix @ initial_state
assert output_state.shape == expected_state.shape, "State dimensions mismatch"
assert np.allclose(output_state, expected_state, atol=1e-6), "QFT state does not match expected state"
Loading