Skip to content

Commit

Permalink
Python typename on double
Browse files Browse the repository at this point in the history
  • Loading branch information
KowerKoint committed Nov 15, 2024
1 parent 965e9a3 commit c9f8ba0
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 121 deletions.
119 changes: 61 additions & 58 deletions include/scaluq/gate/gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,65 +292,67 @@ using Gate = internal::GatePtr<internal::GateBase<Fp>>;

#ifdef SCALUQ_USE_NANOBIND
namespace internal {
#define DEF_GATE_BASE(GATE_TYPE, DESCRIPTION) \
nb::class_<GATE_TYPE>(m, #GATE_TYPE, DESCRIPTION) \
.def("gate_type", &GATE_TYPE::gate_type, "Get gate type as `GateType` enum.") \
.def( \
"target_qubit_list", \
[](const GATE_TYPE& gate) { return gate->target_qubit_list(); }, \
"Get target qubits as `list[int]`. **Control qubits is not included.**") \
.def( \
"control_qubit_list", \
[](const GATE_TYPE& gate) { return gate->control_qubit_list(); }, \
"Get control qubits as `list[int]`.") \
.def( \
"operand_qubit_list", \
[](const GATE_TYPE& gate) { return gate->operand_qubit_list(); }, \
"Get target and control qubits as `list[int]`.") \
.def( \
"target_qubit_mask", \
[](const GATE_TYPE& gate) { return gate->target_qubit_mask(); }, \
"Get target qubits as mask. **Control qubits is not included.**") \
.def( \
"control_qubit_mask", \
[](const GATE_TYPE& gate) { return gate->control_qubit_mask(); }, \
"Get control qubits as mask.") \
.def( \
"operand_qubit_mask", \
[](const GATE_TYPE& gate) { return gate->operand_qubit_mask(); }, \
"Get target and control qubits as mask.") \
.def( \
"get_inverse", \
[](const GATE_TYPE& gate) { return gate->get_inverse(); }, \
"Generate inverse gate as `Gate` type. If not exists, return None.") \
.def( \
"update_quantum_state", \
[](const GATE_TYPE& gate, StateVector<double>& state_vector) { \
gate->update_quantum_state(state_vector); \
}, \
"Apply gate to `state_vector`. `state_vector` in args is directly updated.") \
.def( \
"get_matrix", \
[](const GATE_TYPE& gate) { return gate->get_matrix(); }, \
"Get matrix representation of the gate.") \
.def( \
"to_string", \
[](const GATE_TYPE& gate) { return gate->to_string(""); }, \
"Get string representation of the gate.") \
.def( \
"__str__", \
[](const GATE_TYPE& gate) { return gate->to_string(""); }, \
#define DEF_GATE_BASE(GATE_TYPE, FLOAT, DESCRIPTION) \
nb::class_<GATE_TYPE<FLOAT>>(m, #GATE_TYPE "_" #FLOAT, DESCRIPTION) \
.def("gate_type", &GATE_TYPE<FLOAT>::gate_type, "Get gate type as `GateType` enum.") \
.def( \
"target_qubit_list", \
[](const GATE_TYPE<FLOAT>& gate) { return gate->target_qubit_list(); }, \
"Get target qubits as `list[int]`. **Control qubits is not included.**") \
.def( \
"control_qubit_list", \
[](const GATE_TYPE<FLOAT>& gate) { return gate->control_qubit_list(); }, \
"Get control qubits as `list[int]`.") \
.def( \
"operand_qubit_list", \
[](const GATE_TYPE<FLOAT>& gate) { return gate->operand_qubit_list(); }, \
"Get target and control qubits as `list[int]`.") \
.def( \
"target_qubit_mask", \
[](const GATE_TYPE<FLOAT>& gate) { return gate->target_qubit_mask(); }, \
"Get target qubits as mask. **Control qubits is not included.**") \
.def( \
"control_qubit_mask", \
[](const GATE_TYPE<FLOAT>& gate) { return gate->control_qubit_mask(); }, \
"Get control qubits as mask.") \
.def( \
"operand_qubit_mask", \
[](const GATE_TYPE<FLOAT>& gate) { return gate->operand_qubit_mask(); }, \
"Get target and control qubits as mask.") \
.def( \
"get_inverse", \
[](const GATE_TYPE<FLOAT>& gate) { return gate->get_inverse(); }, \
"Generate inverse gate as `Gate` type. If not exists, return None.") \
.def( \
"update_quantum_state", \
[](const GATE_TYPE<FLOAT>& gate, StateVector<FLOAT>& state_vector) { \
gate->update_quantum_state(state_vector); \
}, \
"Apply gate to `state_vector`. `state_vector` in args is directly updated.") \
.def( \
"get_matrix", \
[](const GATE_TYPE<FLOAT>& gate) { return gate->get_matrix(); }, \
"Get matrix representation of the gate.") \
.def( \
"to_string", \
[](const GATE_TYPE<FLOAT>& gate) { return gate->to_string(""); }, \
"Get string representation of the gate.") \
.def( \
"__str__", \
[](const GATE_TYPE<FLOAT>& gate) { return gate->to_string(""); }, \
"Get string representation of the gate.")

nb::class_<Gate<double>> gate_base_def;
nb::class_<Gate<double>> gate_base_def_double;

#define DEF_GATE(GATE_TYPE, DESCRIPTION) \
::scaluq::internal::gate_base_def.def(nb::init<GATE_TYPE>(), "Upcast from `" #GATE_TYPE "`."); \
DEF_GATE_BASE( \
GATE_TYPE, \
DESCRIPTION \
"\n\n.. note:: Upcast is required to use gate-general functions (ex: add to Circuit).") \
.def(nb::init<Gate<double>>())
#define DEF_GATE(GATE_TYPE, FLOAT, DESCRIPTION) \
::scaluq::internal::gate_base_def_##FLOAT.def(nb::init<GATE_TYPE<FLOAT>>(), \
"Upcast from `" #GATE_TYPE "`."); \
DEF_GATE_BASE( \
GATE_TYPE, \
FLOAT, \
DESCRIPTION \
"\n\n.. note:: Upcast is required to use gate-general functions (ex: add to Circuit).") \
.def(nb::init<Gate<FLOAT>>())

void bind_gate_gate_hpp(nb::module_& m) {
nb::enum_<GateType>(m, "GateType", "Enum of Gate Type.")
Expand Down Expand Up @@ -382,8 +384,9 @@ void bind_gate_gate_hpp(nb::module_& m) {
.value("Pauli", GateType::Pauli)
.value("PauliRotation", GateType::PauliRotation);

gate_base_def =
DEF_GATE_BASE(Gate<double>,
gate_base_def_double =
DEF_GATE_BASE(Gate,
double,
"General class of QuantumGate.\n\n.. note:: Downcast to requred to use "
"gate-specific functions.")
.def(nb::init<Gate<double>>(), "Just copy shallowly.");
Expand Down
8 changes: 4 additions & 4 deletions include/scaluq/gate/gate_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ using DenseMatrixGate = internal::GatePtr<internal::DenseMatrixGateImpl<Fp>>;
#ifdef SCALUQ_USE_NANOBIND
namespace internal {
void bind_gate_gate_matrix_hpp(nb::module_& m) {
DEF_GATE(OneTargetMatrixGate<double>, "Specific class of one-qubit dense matrix gate.")
DEF_GATE(OneTargetMatrixGate, double, "Specific class of one-qubit dense matrix gate.")
.def("matrix", [](const OneTargetMatrixGate<double>& gate) { return gate->matrix(); });
DEF_GATE(TwoTargetMatrixGate<double>, "Specific class of two-qubit dense matrix gate.")
DEF_GATE(TwoTargetMatrixGate, double, "Specific class of two-qubit dense matrix gate.")
.def("matrix", [](const TwoTargetMatrixGate<double>& gate) { return gate->matrix(); });
DEF_GATE(SparseMatrixGate<double>, "Specific class of sparse matrix gate.")
DEF_GATE(SparseMatrixGate, double, "Specific class of sparse matrix gate.")
.def("matrix", [](const SparseMatrixGate<double>& gate) { return gate->get_matrix(); })
.def("sparse_matrix",
[](const SparseMatrixGate<double>& gate) { return gate->get_sparse_matrix(); });
DEF_GATE(DenseMatrixGate<double>, "Specific class of dense matrix gate.")
DEF_GATE(DenseMatrixGate, double, "Specific class of dense matrix gate.")
.def("matrix", [](const DenseMatrixGate<double>& gate) { return gate->get_matrix(); });
}
} // namespace internal
Expand Down
6 changes: 4 additions & 2 deletions include/scaluq/gate/gate_pauli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ using PauliRotationGate = internal::GatePtr<internal::PauliRotationGateImpl<Fp>>
#ifdef SCALUQ_USE_NANOBIND
namespace internal {
void bind_gate_gate_pauli_hpp(nb::module_& m) {
DEF_GATE(PauliGate<double>,
DEF_GATE(PauliGate,
double,
"Specific class of multi-qubit pauli gate, which applies single-qubit Pauli "
"gate to "
"each of qubit.");
DEF_GATE(PauliRotationGate<double>,
DEF_GATE(PauliRotationGate,
double,
"Specific class of multi-qubit pauli-rotation gate, represented as "
"$e^{-i\\frac{\\mathrm{angle}}{2}P}$.");
}
Expand Down
3 changes: 2 additions & 1 deletion include/scaluq/gate/gate_probablistic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ using ProbablisticGate = internal::GatePtr<internal::ProbablisticGateImpl<Fp>>;
#ifdef SCALUQ_USE_NANOBIND
namespace internal {
void bind_gate_gate_probablistic(nb::module_& m) {
DEF_GATE(ProbablisticGate<double>,
DEF_GATE(ProbablisticGate,
double,
"Specific class of probablistic gate. The gate to apply is picked from a cirtain "
"distribution.")
.def(
Expand Down
69 changes: 42 additions & 27 deletions include/scaluq/gate/gate_standard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,66 +467,80 @@ using SwapGate = internal::GatePtr<internal::SwapGateImpl<Fp>>;
#ifdef SCALUQ_USE_NANOBIND
namespace internal {
void bind_gate_gate_standard_hpp(nb::module_& m) {
DEF_GATE(IGate<double>, "Specific class of Pauli-I gate.");
DEF_GATE(GlobalPhaseGate<double>,
DEF_GATE(IGate, double, "Specific class of Pauli-I gate.");
DEF_GATE(GlobalPhaseGate,
double,
"Specific class of gate, which rotate global phase, represented as "
"$e^{i\\mathrm{phase}}I$.")
.def(
"phase",
[](const GlobalPhaseGate<double>& gate) { return gate->phase(); },
"Get `phase` property");
DEF_GATE(XGate<double>, "Specific class of Pauli-X gate.");
DEF_GATE(YGate<double>, "Specific class of Pauli-Y gate.");
DEF_GATE(ZGate<double>, "Specific class of Pauli-Z gate.");
DEF_GATE(HGate<double>, "Specific class of Hadamard gate.");
DEF_GATE(SGate<double>,
DEF_GATE(XGate, double, "Specific class of Pauli-X gate.");
DEF_GATE(YGate, double, "Specific class of Pauli-Y gate.");
DEF_GATE(ZGate, double, "Specific class of Pauli-Z gate.");
DEF_GATE(HGate, double, "Specific class of Hadamard gate.");
DEF_GATE(SGate,
double,
"Specific class of S gate, represented as $\\begin { bmatrix }\n1 & 0\\\\\n0 &"
"i\n\\end{bmatrix}$.");
DEF_GATE(SdagGate<double>, "Specific class of inverse of S gate.");
DEF_GATE(TGate<double>,
DEF_GATE(SdagGate, double, "Specific class of inverse of S gate.");
DEF_GATE(TGate,
double,
"Specific class of T gate, represented as $\\begin { bmatrix }\n1 & 0\\\\\n0 &"
"e^{i\\pi/4}\n\\end{bmatrix}$.");
DEF_GATE(TdagGate<double>, "Specific class of inverse of T gate.");
DEF_GATE(TdagGate, double, "Specific class of inverse of T gate.");
DEF_GATE(
SqrtXGate<double>,
SqrtXGate,
double,
"Specific class of sqrt(X) gate, represented as $\\begin{ bmatrix }\n1+i & 1-i\\\\\n1-i "
"& 1+i\n\\end{bmatrix}$.");
DEF_GATE(SqrtXdagGate<double>, "Specific class of inverse of sqrt(X) gate.");
DEF_GATE(SqrtYGate<double>,
DEF_GATE(SqrtXdagGate, double, "Specific class of inverse of sqrt(X) gate.");
DEF_GATE(SqrtYGate,
double,
"Specific class of sqrt(Y) gate, represented as $\\begin{ bmatrix }\n1+i & -1-i "
"\\\\\n1+i & 1+i\n\\end{bmatrix}$.");
DEF_GATE(SqrtYdagGate<double>, "Specific class of inverse of sqrt(Y) gate.");
DEF_GATE(SqrtYdagGate, double, "Specific class of inverse of sqrt(Y) gate.");
DEF_GATE(
P0Gate<double>,
P0Gate,
double,
"Specific class of projection gate to $\\ket{0}$.\n\n.. note:: This gate is not unitary.");
DEF_GATE(
P1Gate<double>,
P1Gate,
double,
"Specific class of projection gate to $\\ket{1}$.\n\n.. note:: This gate is not unitary.");

#define DEF_ROTATION_GATE(GATE_TYPE, DESCRIPTION) \
DEF_GATE(GATE_TYPE, DESCRIPTION) \
.def( \
"angle", [](const GATE_TYPE& gate) { return gate->angle(); }, "Get `angle` property.")
#define DEF_ROTATION_GATE(GATE_TYPE, FLOAT, DESCRIPTION) \
DEF_GATE(GATE_TYPE, FLOAT, DESCRIPTION) \
.def( \
"angle", \
[](const GATE_TYPE<FLOAT>& gate) { return gate->angle(); }, \
"Get `angle` property.")

DEF_ROTATION_GATE(
RXGate<double>,
RXGate,
double,
"Specific class of X rotation gate, represented as $e^{-i\\frac{\\mathrm{angle}}{2}X}$.");
DEF_ROTATION_GATE(
RYGate<double>,
RYGate,
double,
"Specific class of Y rotation gate, represented as $e^{-i\\frac{\\mathrm{angle}}{2}Y}$.");
DEF_ROTATION_GATE(
RZGate<double>,
RZGate,
double,
"Specific class of Z rotation gate, represented as $e^{-i\\frac{\\mathrm{angle}}{2}Z}$.");

DEF_GATE(U1Gate<double>,
DEF_GATE(U1Gate,
double,
"Specific class of IBMQ's U1 Gate, which is a rotation abount Z-axis, "
"represented as "
"$\\begin{bmatrix}\n1 & 0\\\\\n0 & e^{i\\lambda}\n\\end{bmatrix}$.")
.def(
"lambda_",
[](const U1Gate<double>& gate) { return gate->lambda(); },
"Get `lambda` property.");
DEF_GATE(U2Gate<double>,
DEF_GATE(U2Gate,
double,
"Specific class of IBMQ's U2 Gate, which is a rotation about X+Z-axis, "
"represented as "
"$\\frac{1}{\\sqrt{2}} \\begin{bmatrix}1 & -e^{-i\\lambda}\\\\\n"
Expand All @@ -537,7 +551,8 @@ void bind_gate_gate_standard_hpp(nb::module_& m) {
"lambda_",
[](const U2Gate<double>& gate) { return gate->lambda(); },
"Get `lambda` property.");
DEF_GATE(U3Gate<double>,
DEF_GATE(U3Gate,
double,
"Specific class of IBMQ's U3 Gate, which is a rotation abount 3 axis, "
"represented as "
"$\\begin{bmatrix}\n\\cos \\frac{\\theta}{2} & "
Expand All @@ -554,7 +569,7 @@ void bind_gate_gate_standard_hpp(nb::module_& m) {
"lambda_",
[](const U3Gate<double>& gate) { return gate->lambda(); },
"Get `lambda` property.");
DEF_GATE(SwapGate<double>, "Specific class of two-qubit swap gate.");
DEF_GATE(SwapGate, double, "Specific class of two-qubit swap gate.");
}
} // namespace internal
#endif
Expand Down
Loading

0 comments on commit c9f8ba0

Please sign in to comment.