Skip to content

Commit

Permalink
added boundary check
Browse files Browse the repository at this point in the history
  • Loading branch information
gandalfr-KY committed Mar 7, 2024
1 parent 3480ddf commit ae09c8d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions qulacs/gate/gate_npair_qubit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions qulacs/gate/gate_one_control_one_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions qulacs/gate/gate_one_qubit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,74 +10,92 @@ Gate SqrtXGateImpl::get_inverse() const { return std::make_shared<SqrtXdagGateIm
Gate SqrtYGateImpl::get_inverse() const { return std::make_shared<SqrtYdagGateImpl>(_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
Expand Down
3 changes: 3 additions & 0 deletions qulacs/gate/gate_quantum_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions qulacs/gate/gate_two_qubit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions qulacs/gate/update_ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ae09c8d

Please sign in to comment.