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

Bugfix Catalyst: add GlobalPhase in TOML files #615

Merged
merged 10 commits into from
Feb 22, 2024
6 changes: 6 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

### Improvements

* Add adjoint support for `GlobalPhase` in Lightning-GPU and Lightning-Kokkos.
[(#615)](https://github.com/PennyLaneAI/pennylane-lightning/pull/615)

* Lower the overheads of Windows CI tests.
[(#610)](https://github.com/PennyLaneAI/pennylane-lightning/pull/610)

Expand Down Expand Up @@ -49,6 +52,9 @@

### Bug fixes

* List `GlobalPhase` gate in each device's TOML file.
[(#615)](https://github.com/PennyLaneAI/pennylane-lightning/pull/615)

* Lightning-GPU's gate cache failed to distinguish between certain gates.
For example, `MultiControlledX([0, 1, 2], "111")` and `MultiControlledX([0, 2], "00")` were applied as the same operation.
This could happen with (at least) the following gates: `QubitUnitary`,`ControlledQubitUnitary`,`MultiControlledX`,`DiagonalQubitUnitary`,`PSWAP`,`OrbitalRotation`.
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ test-cpp:
cmake -BBuildTests -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=ON -DENABLE_WARNINGS=ON -DPL_BACKEND=$(PL_BACKEND) $(OPTIONS)
ifdef target
cmake --build ./BuildTests $(VERBOSE) --target $(target)
OMP_PROC_BIND=false ./BuildTests/$(target)
./BuildTests/$(target)
else
cmake --build ./BuildTests $(VERBOSE)
OMP_PROC_BIND=false cmake --build ./BuildTests $(VERBOSE) --target test
cmake --build ./BuildTests $(VERBOSE) --target test
mlxd marked this conversation as resolved.
Show resolved Hide resolved
endif

.PHONY: format format-cpp
Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.35.0-dev15"
__version__ = "0.35.0-dev16"
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,20 @@ class StateVectorCudaManaged
applyParametricPauliGate(names, {}, wires, param, adjoint);
}

/* Gate generators */
/**
* @brief Gradient generator function associated with the GlobalPhase gate.
*
* @param sv Statevector
* @param wires Wires to apply operation.
* @param adj Takes adjoint of operation if true. Defaults to false.
*/
inline PrecisionT
applyGeneratorGlobalPhase([[maybe_unused]] const std::vector<size_t> &wires,
[[maybe_unused]] bool adj = false) {
return static_cast<PrecisionT>(-1.0);
}

/* Gate generators */
/**
* @brief Gradient generator function associated with the RX gate.
Expand Down Expand Up @@ -1149,6 +1163,12 @@ class StateVectorCudaManaged

// Holds the mapping from gate labels to associated generator functions.
const GMap generator_map_{
{"GlobalPhase",
[&](auto &&wires, auto &&adjoint) {
return applyGeneratorGlobalPhase(
std::forward<decltype(wires)>(wires),
std::forward<decltype(adjoint)>(adjoint));
}},
{"RX",
[&](auto &&wires, auto &&adjoint) {
return applyGeneratorRX(std::forward<decltype(wires)>(wires),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1644,4 +1644,65 @@ TEST_CASE("Generators::applyGeneratorMultiRZ", "[GateGenerators]") {
}
}
}
}
}

TEMPLATE_TEST_CASE("StateVectorCudaManaged::applyGeneratorGlobalPhase",
"[StateVectorCudaManaged_Generator]", float, double) {
const bool inverse = GENERATE(true, false);
const std::string gate_name = "GlobalPhase";
{
using ComplexT = StateVectorCudaManaged<TestType>::ComplexT;
const size_t num_qubits = 4;
const TestType ep = 1e-3;
const TestType EP = 1e-4;

std::vector<ComplexT> ini_st{
ComplexT{0.267462841882, 0.010768564798},
ComplexT{0.228575129706, 0.010564590956},
ComplexT{0.099492749900, 0.260849823392},
ComplexT{0.093690204310, 0.189847108173},
ComplexT{0.033390732374, 0.203836830144},
ComplexT{0.226979395737, 0.081852150975},
ComplexT{0.031235505729, 0.176933497281},
ComplexT{0.294287602843, 0.145156781198},
ComplexT{0.152742706049, 0.111628061129},
ComplexT{0.012553863703, 0.120027860480},
ComplexT{0.237156555364, 0.154658769755},
ComplexT{0.117001120872, 0.228059505033},
ComplexT{0.041495873225, 0.065934827444},
ComplexT{0.089653239407, 0.221581340372},
ComplexT{0.217892322429, 0.291261296999},
ComplexT{0.292993251871, 0.186570798697},
};

StateVectorCudaManaged<TestType> gntr_sv{ini_st.data(), ini_st.size()};
StateVectorCudaManaged<TestType> gate_svp{ini_st.data(), ini_st.size()};
StateVectorCudaManaged<TestType> gate_svm{ini_st.data(), ini_st.size()};

auto scale = gntr_sv.applyGenerator(gate_name, {0}, inverse);
if (inverse) {
gate_svp.applyOperation(gate_name, {0}, inverse, {-ep});
gate_svm.applyOperation(gate_name, {0}, inverse, {ep});
} else {
gate_svp.applyOperation(gate_name, {0}, inverse, {ep});
gate_svm.applyOperation(gate_name, {0}, inverse, {-ep});
}

auto result_gntr_sv = gntr_sv.getDataVector();
auto result_gate_svp = gate_svp.getDataVector();
auto result_gate_svm = gate_svm.getDataVector();

for (size_t j = 0; j < exp2(num_qubits); j++) {
CHECK(-scale * imag(result_gntr_sv[j]) ==
Approx(0.5 *
(real(result_gate_svp[j]) - real(result_gate_svm[j])) /
ep)
.margin(EP));
CHECK(scale * real(result_gntr_sv[j]) ==
Approx(0.5 *
(imag(result_gate_svp[j]) - imag(result_gate_svm[j])) /
ep)
.margin(EP));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,8 @@ class StateVectorKokkos final
return -static_cast<fp_t>(0.5);
case GeneratorOperation::MultiRZ:
return applyGeneratorMultiRZ(wires, inverse, params);
case GeneratorOperation::GlobalPhase:
return static_cast<PrecisionT>(-1.0);
/// LCOV_EXCL_START
default:
PL_ABORT(std::string("Generator does not exist for ") + opName);
Expand Down Expand Up @@ -924,6 +926,7 @@ class StateVectorKokkos final
generators_indices_["DoubleExcitationPlus"] = GeneratorOperation::DoubleExcitationPlus;
generators_indices_["PhaseShift"] = GeneratorOperation::PhaseShift;
generators_indices_["MultiRZ"] = GeneratorOperation::MultiRZ;
generators_indices_["GlobalPhase"] = GeneratorOperation::GlobalPhase;
}
// clang-format on
};
Expand Down
Loading
Loading