Skip to content

Commit

Permalink
Update support
Browse files Browse the repository at this point in the history
  • Loading branch information
maliasadi committed Sep 24, 2024
1 parent 6cb536e commit 1104ab7
Show file tree
Hide file tree
Showing 28 changed files with 85 additions and 45 deletions.
8 changes: 6 additions & 2 deletions mpitests/test_adjoint_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,9 @@ def test_qubit_unitary(dev, n_targets):

np.random.seed(1337)
par = 2 * np.pi * np.random.rand(n_wires)
U = np.random.rand(2**n_targets, 2**n_targets) + 1j * np.random.rand(2**n_targets, 2**n_targets)
U = np.random.rand(2**n_targets, 2**n_targets) + 1j * np.random.rand(
2**n_targets, 2**n_targets
)
U, _ = np.linalg.qr(U)
init_state = np.random.rand(2**n_wires) + 1j * np.random.rand(2**n_wires)
init_state /= np.sqrt(np.dot(np.conj(init_state), init_state))
Expand Down Expand Up @@ -1432,7 +1434,9 @@ def test_diff_qubit_unitary(dev, n_targets):

np.random.seed(1337)
par = 2 * np.pi * np.random.rand(n_wires)
U = np.random.rand(2**n_targets, 2**n_targets) + 1j * np.random.rand(2**n_targets, 2**n_targets)
U = np.random.rand(2**n_targets, 2**n_targets) + 1j * np.random.rand(
2**n_targets, 2**n_targets
)
U, _ = np.linalg.qr(U)
init_state = np.random.rand(2**n_wires) + 1j * np.random.rand(2**n_wires)
init_state /= np.sqrt(np.dot(np.conj(init_state), init_state))
Expand Down
4 changes: 3 additions & 1 deletion pennylane_lightning/core/_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ def serialize_observables(self, tape: QuantumTape, wires_map: dict = None) -> Li
obs_indices.append(i)
return serialized_obs, obs_indices

def serialize_ops(self, tape: QuantumTape, wires_map: dict = None) -> Tuple[
def serialize_ops(
self, tape: QuantumTape, wires_map: dict = None
) -> Tuple[
List[List[str]],
List[np.ndarray],
List[List[int]],
Expand Down
1 change: 0 additions & 1 deletion pennylane_lightning/core/_state_vector_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class LightningBaseStateVector(ABC):
"""

def __init__(self, num_wires: int, dtype: Union[np.complex128, np.complex64]):

if dtype not in [np.complex64, np.complex128]:
raise TypeError(f"Unsupported complex type: {dtype}")

Expand Down
12 changes: 6 additions & 6 deletions pennylane_lightning/core/src/algorithms/JacobianData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ template <class StateVectorT> class OpsData {
std::vector<std::vector<bool>> ops_controlled_values)
: num_par_ops_{0}, ops_name_{std::move(ops_name)},
ops_params_{ops_params}, ops_wires_{std::move(ops_wires)},
ops_inverses_{std::move(ops_inverses)}, ops_matrices_{std::move(
ops_matrices)},
ops_inverses_{std::move(ops_inverses)},
ops_matrices_{std::move(ops_matrices)},
ops_controlled_wires_{std::move(ops_controlled_wires)},
ops_controlled_values_{std::move(ops_controlled_values)} {
for (const auto &p : ops_params) {
Expand Down Expand Up @@ -105,8 +105,8 @@ template <class StateVectorT> class OpsData {
std::vector<std::vector<ComplexT>> ops_matrices)
: num_par_ops_{0}, ops_name_{std::move(ops_name)},
ops_params_{ops_params}, ops_wires_{std::move(ops_wires)},
ops_inverses_{std::move(ops_inverses)}, ops_matrices_{std::move(
ops_matrices)},
ops_inverses_{std::move(ops_inverses)},
ops_matrices_{std::move(ops_matrices)},
ops_controlled_wires_(ops_name.size()),
ops_controlled_values_(ops_name.size()) {
for (const auto &p : ops_params) {
Expand All @@ -130,8 +130,8 @@ template <class StateVectorT> class OpsData {
std::vector<std::vector<std::size_t>> ops_wires,
std::vector<bool> ops_inverses)
: num_par_ops_{0}, ops_name_{ops_name}, ops_params_{ops_params},
ops_wires_{std::move(ops_wires)}, ops_inverses_{std::move(
ops_inverses)},
ops_wires_{std::move(ops_wires)},
ops_inverses_{std::move(ops_inverses)},
ops_matrices_(ops_name.size()),
ops_controlled_wires_(ops_name.size()),
ops_controlled_values_(ops_name.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,19 @@ void applyGlobalPhase(Kokkos::View<Kokkos::complex<PrecisionT> *> arr_,
KOKKOS_LAMBDA(const std::size_t k) { arr_(k) *= phase; });
}

template <class ExecutionSpace, class PrecisionT>
void applyPCPhase(Kokkos::View<Kokkos::complex<PrecisionT> *> arr_,
const std::size_t num_qubits,
[[maybe_unused]] const std::vector<std::size_t> &wires,
const bool inverse = false,
const std::vector<PrecisionT> &params = {}) {
const Kokkos::complex<PrecisionT> phase = Kokkos::exp(
Kokkos::complex<PrecisionT>{0, (inverse) ? params[0] : -params[0]});
Kokkos::parallel_for(
Kokkos::RangePolicy<ExecutionSpace>(0, exp2(num_qubits)),
KOKKOS_LAMBDA(const std::size_t k) { arr_(k) *= phase; });
}

template <class ExecutionSpace, class PrecisionT>
void applyNamedOperation(const GateOperation gateop,
Kokkos::View<Kokkos::complex<PrecisionT> *> arr_,
Expand Down Expand Up @@ -1277,6 +1290,9 @@ void applyNamedOperation(const GateOperation gateop,
applyGlobalPhase<ExecutionSpace>(arr_, num_qubits, wires, inverse,
params);
return;
case GateOperation::PCPhase:
applyPCPhase<ExecutionSpace>(arr_, num_qubits, wires, inverse, params);
return;
case GateOperation::MultiRZ:
applyMultiRZ<ExecutionSpace>(arr_, num_qubits, wires, inverse, params);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ class StateVectorLQubit : public StateVectorBase<PrecisionT, Derived> {
protected:
explicit StateVectorLQubit(std::size_t num_qubits, Threading threading,
CPUMemoryModel memory_model)
: BaseType(num_qubits), threading_{threading}, memory_model_{
memory_model} {
: BaseType(num_qubits), threading_{threading},
memory_model_{memory_model} {
setKernels(num_qubits, threading, memory_model);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ constexpr auto constructControlledMatrixOpsFunctorTupleIter() {
* @tparam GateImplementation Gate implementation class.
*/
template <class PrecisionT, class ParamT, class GateImplementation>
constexpr auto gate_op_functor_tuple = constructGateOpsFunctorTupleIter<
PrecisionT, ParamT, GateImplementation, 0>();
constexpr auto gate_op_functor_tuple =
constructGateOpsFunctorTupleIter<PrecisionT, ParamT, GateImplementation,
0>();

/**
* @brief Tuple of controlled gate operation and function pointer pairs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,6 @@ class GateImplementationsLM : public PauliGenerator<GateImplementationsLM> {
const std::array<std::complex<PrecisionT>, 2> shifts = {
(inverse) ? std::conj(first) : first,
(inverse) ? std::conj(second) : second};

std::size_t wires_parity{0U};
for (std::size_t wire : wires) {
wires_parity |=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ namespace Internal {
template <typename T> struct AVX2Intrinsic {
static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>);
};
template <> struct AVX2Intrinsic<float> { using Type = __m256; };
template <> struct AVX2Intrinsic<double> { using Type = __m256d; };
template <> struct AVX2Intrinsic<float> {
using Type = __m256;
};
template <> struct AVX2Intrinsic<double> {
using Type = __m256d;
};
} // namespace Internal
///@endcond

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ namespace Internal {
template <typename T> struct AVX512Intrinsic {
static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>);
};
template <> struct AVX512Intrinsic<float> { using Type = __m512; };
template <> struct AVX512Intrinsic<double> { using Type = __m512d; };
template <> struct AVX512Intrinsic<float> {
using Type = __m512;
};
template <> struct AVX512Intrinsic<double> {
using Type = __m512d;
};
} // namespace Internal
///@endcond

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ namespace Pennylane::LightningQubit::Gates::AVXCommon {
template <class PrecisionT, std::size_t packed_size> struct AVXConcept;

#ifdef PL_USE_AVX2
template <> struct AVXConcept<float, 8> { using Type = AVX2Concept<float>; };
template <> struct AVXConcept<double, 4> { using Type = AVX2Concept<double>; };
template <> struct AVXConcept<float, 8> {
using Type = AVX2Concept<float>;
};
template <> struct AVXConcept<double, 4> {
using Type = AVX2Concept<double>;
};
#endif

#ifdef PL_USE_AVX512F
template <> struct AVXConcept<float, 16> { using Type = AVX512Concept<float>; };
template <> struct AVXConcept<float, 16> {
using Type = AVX512Concept<float>;
};
template <> struct AVXConcept<double, 8> {
using Type = AVX512Concept<double>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ concept TwoQubitGateWithParam =

template <class T>
concept TwoQubitGateWithoutParam = SymmetricTwoQubitGateWithoutParam<T> ||
AsymmetricTwoQubitGateWithoutParam<T>;
AsymmetricTwoQubitGateWithoutParam<T>;

namespace Internal {
// InternalInternal for two qubit gates with param begin
Expand Down Expand Up @@ -326,7 +326,7 @@ constexpr auto InternalExternalFunctions() -> decltype(auto) {
* @brief A helper class for two-qubit gate without parameters.
*/
template <class AVXImpl>
requires TwoQubitGateWithoutParam<AVXImpl>
requires TwoQubitGateWithoutParam<AVXImpl>
class TwoQubitGateWithoutParamHelper {
public:
using Precision = typename AVXImpl::Precision;
Expand Down Expand Up @@ -451,7 +451,7 @@ class TwoQubitGateWithoutParamHelper {
* @brief A helper class for two-qubit gate without parameters.
*/
template <class AVXImpl, class ParamT>
requires TwoQubitGateWithParam<AVXImpl>
requires TwoQubitGateWithParam<AVXImpl>
class TwoQubitGateWithParamHelper {
public:
using Precision = typename AVXImpl::Precision;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ TEST_CASE("Test KernelMap is consistent in extreme usecase", "[KernelMap]") {
#ifdef _OPENMP
#pragma omp parallel default(none) \
shared(instance, records, rd, num_qubits, threadings, memory_models) \
firstprivate(num_iter)
firstprivate(num_iter)
#endif
{
std::mt19937 re;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ auto probs_bitshift(const std::complex<PrecisionT> *arr,
std::vector<PrecisionT> probabilities(n_probs, 0);
auto *probs = probabilities.data();
#if defined PL_LQ_KERNEL_OMP && defined _OPENMP
#pragma omp parallel for reduction(+ : probs[:n_probs])
#pragma omp parallel for reduction(+ : probs[ : n_probs])
#endif
for (std::size_t k = 0; k < exp2(num_qubits - n_wires); k++) {
std::size_t i0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ omp_innerProd(const std::complex<T> *v1, const std::complex<T> *v2,
}

#pragma omp parallel for num_threads(nthreads) default(none) \
shared(v1, v2, data_size) reduction(sm \
: result)
shared(v1, v2, data_size) reduction(sm : result)
#endif
for (std::size_t i = 0; i < data_size; i++) {
result = ConstSum(result, ConstMult(*(v1 + i), *(v2 + i)));
Expand Down Expand Up @@ -168,8 +167,7 @@ omp_innerProdC(const std::complex<T> *v1, const std::complex<T> *v2,

#if defined(_OPENMP)
#pragma omp parallel for num_threads(nthreads) default(none) \
shared(v1, v2, data_size) reduction(sm \
: result)
shared(v1, v2, data_size) reduction(sm : result)
#endif
for (std::size_t i = 0; i < data_size; i++) {
result = ConstSum(result, ConstMultConj(*(v1 + i), *(v2 + i)));
Expand Down
8 changes: 6 additions & 2 deletions pennylane_lightning/core/src/utils/TypeTraits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#include <limits>

namespace Pennylane::Util {
template <typename T> struct remove_complex { using type = T; };
template <typename T> struct remove_complex {
using type = T;
};
template <typename T> struct remove_complex<std::complex<T>> {
using type = T;
};
Expand Down Expand Up @@ -54,6 +56,8 @@ template <class F> struct FuncReturn {
template <class R, class... A> struct FuncReturn<R (*)(A...)> {
using Type = R;
};
template <class R, class... A> struct FuncReturn<R(A...)> { using Type = R; };
template <class R, class... A> struct FuncReturn<R(A...)> {
using Type = R;
};

} // namespace Pennylane::Util
4 changes: 2 additions & 2 deletions pennylane_lightning/core/src/utils/cuda_utils/DataBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ template <class GPUDataT, class DevTagT = int> class DataBuffer {

DataBuffer(std::size_t length, int device_id = 0,
cudaStream_t stream_id = 0, bool alloc_memory = true)
: length_{length}, dev_tag_{device_id, stream_id}, gpu_buffer_{
nullptr} {
: length_{length}, dev_tag_{device_id, stream_id},
gpu_buffer_{nullptr} {
if (alloc_memory && (length > 0)) {
dev_tag_.refresh();
PL_CUDA_IS_SUCCESS(
Expand Down
2 changes: 2 additions & 0 deletions pennylane_lightning/lightning_gpu/lightning_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def _mebibytesToBytes(mebibytes):
"MultiRZ",
"GlobalPhase",
"C(GlobalPhase)",
"PCPhase",
"C(PCPhase)",
"Hadamard",
"S",
"Adjoint(S)",
Expand Down
1 change: 1 addition & 0 deletions pennylane_lightning/lightning_gpu/lightning_gpu.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ MultiControlledX = {}

BlockEncode = {properties = [ "controllable" ]}
GlobalPhase = {properties = [ "controllable" ]}
PCPhase = {properties = [ "controllable" ]}
ControlledQubitUnitary = {}
ECR = {}
SX = {}
Expand Down
1 change: 0 additions & 1 deletion pennylane_lightning/lightning_kokkos/_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def __init__(
self,
kokkos_state,
) -> None:

super().__init__(kokkos_state)

self._measurement_lightning = self._measurement_dtype()(kokkos_state.state_vector)
Expand Down
2 changes: 0 additions & 2 deletions pennylane_lightning/lightning_kokkos/_state_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def __init__(
kokkos_args=None,
sync=True,
): # pylint: disable=too-many-arguments

super().__init__(num_wires, dtype)

self._device_name = "lightning.kokkos"
Expand Down Expand Up @@ -280,7 +279,6 @@ def _apply_lightning(
elif isinstance(operation, qml.ops.Controlled) and isinstance(
operation.base, qml.GlobalPhase
): # apply n-controlled gate

# Kokkos do not support the controlled gates except for GlobalPhase
self._apply_lightning_controlled(operation)
else: # apply gate as a matrix
Expand Down
2 changes: 2 additions & 0 deletions pennylane_lightning/lightning_kokkos/lightning_kokkos.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
"MultiRZ",
"GlobalPhase",
"C(GlobalPhase)",
"PCPhase",
"C(PCPhase)",
"Hadamard",
"S",
"Adjoint(S)",
Expand Down
3 changes: 2 additions & 1 deletion pennylane_lightning/lightning_kokkos/lightning_kokkos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ ControlledQubitUnitary = {}
# Gates which should be translated to QubitUnitary
[operators.gates.matrix]

BlockEncode = {}
BlockEncode = {properties = [ "controllable" ]}
PCPhase = {properties = [ "controllable" ]}
DiagonalQubitUnitary = {}
ECR = {}
ISWAP = {}
Expand Down
1 change: 0 additions & 1 deletion pennylane_lightning/lightning_qubit/_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def __init__(
kernel_name: str = None,
num_burnin: int = None,
) -> None:

super().__init__(qubit_state)

self._mcmc = mcmc
Expand Down
1 change: 0 additions & 1 deletion pennylane_lightning/lightning_qubit/_state_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class LightningStateVector(LightningBaseStateVector): # pylint: disable=too-few
"""

def __init__(self, num_wires, dtype=np.complex128):

super().__init__(num_wires, dtype)

self._device_name = "lightning.qubit"
Expand Down
1 change: 0 additions & 1 deletion tests/lightning_qubit/test_jacobian_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def calculate_reference(tape, execute_and_derivatives=False):

@staticmethod
def process_and_execute(statevector, tape, execute_and_derivatives=False):

wires = statevector.num_wires
device = LightningDevice(wires)
if execute_and_derivatives:
Expand Down
8 changes: 6 additions & 2 deletions tests/test_adjoint_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,9 @@ def test_qubit_unitary(n_targets):
init_state = np.random.rand(2**n_wires) + 1j * np.random.rand(2**n_wires)
init_state /= np.linalg.norm(init_state)
init_state = np.array(init_state, requires_grad=False)
U = np.random.rand(2**n_targets, 2**n_targets) + 1j * np.random.rand(2**n_targets, 2**n_targets)
U = np.random.rand(2**n_targets, 2**n_targets) + 1j * np.random.rand(
2**n_targets, 2**n_targets
)
U, _ = np.linalg.qr(U)
U = np.array(U, requires_grad=False)

Expand Down Expand Up @@ -1592,7 +1594,9 @@ def test_diff_qubit_unitary(n_targets):
init_state = np.random.rand(2**n_wires) + 1j * np.random.rand(2**n_wires)
init_state /= np.linalg.norm(init_state)
init_state = np.array(init_state, requires_grad=False)
U = np.random.rand(2**n_targets, 2**n_targets) + 1j * np.random.rand(2**n_targets, 2**n_targets)
U = np.random.rand(2**n_targets, 2**n_targets) + 1j * np.random.rand(
2**n_targets, 2**n_targets
)
U, _ = np.linalg.qr(U)
U = np.array(U, requires_grad=False)

Expand Down
1 change: 0 additions & 1 deletion tests/test_native_mcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def func(x, y):
):
func(*params)
if device_name == "lightning.kokkos":

with pytest.raises(
qml.DeviceError,
match=r"Measurement shadow\(wires=\[0\]\) not accepted with finite shots on "
Expand Down

0 comments on commit 1104ab7

Please sign in to comment.