From ae09c8d23287c8eb64d3eba45273a425192406b2 Mon Sep 17 00:00:00 2001 From: gandalfr-ky Date: Thu, 7 Mar 2024 09:28:37 +0000 Subject: [PATCH] added boundary check --- qulacs/gate/gate_npair_qubit.cpp | 2 ++ qulacs/gate/gate_one_control_one_target.cpp | 4 ++++ qulacs/gate/gate_one_qubit.cpp | 18 ++++++++++++++++++ qulacs/gate/gate_quantum_matrix.cpp | 3 +++ qulacs/gate/gate_two_qubit.cpp | 2 ++ qulacs/gate/update_ops.hpp | 7 +++++++ 6 files changed, 36 insertions(+) diff --git a/qulacs/gate/gate_npair_qubit.cpp b/qulacs/gate/gate_npair_qubit.cpp index a91b6255..25ee8791 100644 --- a/qulacs/gate/gate_npair_qubit.cpp +++ b/qulacs/gate/gate_npair_qubit.cpp @@ -5,6 +5,8 @@ namespace qulacs { namespace internal { void FusedSwapGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_qubit_index1 + this->_block_size - 1); + check_qubit_within_bounds(state_vector, this->_qubit_index2 + this->_block_size - 1); fusedswap_gate(this->_qubit_index1, this->_qubit_index2, this->_block_size, state_vector); } } // namespace internal diff --git a/qulacs/gate/gate_one_control_one_target.cpp b/qulacs/gate/gate_one_control_one_target.cpp index 6fa42fb4..adfcd16d 100644 --- a/qulacs/gate/gate_one_control_one_target.cpp +++ b/qulacs/gate/gate_one_control_one_target.cpp @@ -5,10 +5,14 @@ namespace qulacs { namespace internal { void CXGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_control); + check_qubit_within_bounds(state_vector, this->_target); cx_gate(this->_control, this->_target, state_vector); } void CZGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_control); + check_qubit_within_bounds(state_vector, this->_target); cz_gate(this->_control, this->_target, state_vector); } } // namespace internal diff --git a/qulacs/gate/gate_one_qubit.cpp b/qulacs/gate/gate_one_qubit.cpp index 428f4197..3465adf8 100644 --- a/qulacs/gate/gate_one_qubit.cpp +++ b/qulacs/gate/gate_one_qubit.cpp @@ -10,74 +10,92 @@ Gate SqrtXGateImpl::get_inverse() const { return std::make_shared(_target); } void IGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); i_gate(this->_target, state_vector); } void XGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); x_gate(this->_target, state_vector); } void YGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); y_gate(this->_target, state_vector); } void ZGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); z_gate(this->_target, state_vector); } void HGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); h_gate(this->_target, state_vector); } void SGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); s_gate(this->_target, state_vector); } void SdagGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); sdag_gate(this->_target, state_vector); } void TGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); t_gate(this->_target, state_vector); } void TdagGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); tdag_gate(this->_target, state_vector); } void SqrtXGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); sqrtx_gate(this->_target, state_vector); } void SqrtXdagGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); sqrtxdag_gate(this->_target, state_vector); } void SqrtYGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); sqrty_gate(this->_target, state_vector); } void SqrtYdagGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); sqrtydag_gate(this->_target, state_vector); } void P0GateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); p0_gate(this->_target, state_vector); } void P1GateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); p1_gate(this->_target, state_vector); } void RXGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); rx_gate(this->_target, this->_angle, state_vector); } void RYGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); ry_gate(this->_target, this->_angle, state_vector); } void RZGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); rz_gate(this->_target, this->_angle, state_vector); } } // namespace internal diff --git a/qulacs/gate/gate_quantum_matrix.cpp b/qulacs/gate/gate_quantum_matrix.cpp index 487e8efc..0618eef0 100644 --- a/qulacs/gate/gate_quantum_matrix.cpp +++ b/qulacs/gate/gate_quantum_matrix.cpp @@ -3,14 +3,17 @@ namespace qulacs { namespace internal { void U1GateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); u_gate(this->_target, this->_matrix, state_vector); } void U2GateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); u_gate(this->_target, this->_matrix, state_vector); } void U3GateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target); u_gate(this->_target, this->_matrix, state_vector); } } // namespace internal diff --git a/qulacs/gate/gate_two_qubit.cpp b/qulacs/gate/gate_two_qubit.cpp index 70eac3b9..f7ab2b8d 100644 --- a/qulacs/gate/gate_two_qubit.cpp +++ b/qulacs/gate/gate_two_qubit.cpp @@ -5,6 +5,8 @@ namespace qulacs { namespace internal { void SwapGateImpl::update_quantum_state(StateVector& state_vector) const { + check_qubit_within_bounds(state_vector, this->_target1); + check_qubit_within_bounds(state_vector, this->_target2); swap_gate(this->_target1, this->_target2, state_vector); } } // namespace internal diff --git a/qulacs/gate/update_ops.hpp b/qulacs/gate/update_ops.hpp index 04184ab1..6932ae2f 100644 --- a/qulacs/gate/update_ops.hpp +++ b/qulacs/gate/update_ops.hpp @@ -6,6 +6,13 @@ namespace qulacs { namespace internal { +inline void check_qubit_within_bounds(const StateVector& state, UINT op_qubit) { + if (op_qubit >= state.n_qubits()) [[unlikely]] { + throw std::runtime_error( + "Target/Control qubit exceeds the number of qubits in the system."); + } +} + void i_gate(UINT target_qubit_index, StateVector& state); void x_gate(UINT target_qubit_index, StateVector& state);