Skip to content

Commit

Permalink
fix update_ops_one_control_one_target.cpp, gate_test.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Glacialte committed Jan 9, 2024
1 parent 00a9476 commit 6c992ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
16 changes: 7 additions & 9 deletions qulacs/gate/update_ops_one_control_one_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ void cnot_gate(UINT control_qubit_index, UINT target_qubit_index, StateVector& s
const UINT n_qubits = state.n_qubits();
const UINT target_mask = 1ULL << target_qubit_index;
const UINT control_mask = 1ULL << control_qubit_index;
const UINT min_qubit_index = std::min(control_qubit_index, target_qubit_index);
const UINT max_qubit_index = std::max(control_qubit_index, target_qubit_index);
auto [min_qubit_index, max_qubit_index] =
Kokkos::minmax(control_qubit_index, target_qubit_index);
const UINT min_qubit_mask = 1ULL << min_qubit_index;
const UINT max_qubit_mask = 1ULL << (max_qubit_index - 1);
const UINT low_mask = min_qubit_mask - 1;
Expand All @@ -32,13 +32,11 @@ void cz_gate(UINT control_qubit_index, UINT target_qubit_index, StateVector& sta
const UINT n_qubits = state.n_qubits();
const UINT target_mask = 1ULL << target_qubit_index;
const UINT control_mask = 1ULL << control_qubit_index;
const UINT min_qubit_index = std::min(control_qubit_index, target_qubit_index);
const UINT max_qubit_index = std::max(control_qubit_index, target_qubit_index);
const UINT min_qubit_mask = 1ULL << min_qubit_index;
const UINT max_qubit_mask = 1ULL << (max_qubit_index - 1);
const UINT low_mask = min_qubit_mask - 1;
const UINT mid_mask = (max_qubit_mask - 1) ^ low_mask;
const UINT high_mask = ~(max_qubit_mask - 1);
auto [min_qubit_index, max_qubit_index] =
Kokkos::minmax(control_qubit_index, target_qubit_index);
const UINT low_mask = (1ULL << min_qubit_index) - 1;
const UINT mid_mask = ((1ULL << (max_qubit_index - 1)) - 1) ^ low_mask;
const UINT high_mask = ~(low_mask | mid_mask);

auto amplitudes = state.amplitudes_raw();
Kokkos::parallel_for(
Expand Down
22 changes: 16 additions & 6 deletions tests/gate/gate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ void run_random_gate_apply(UINT n_qubits,
Eigen::VectorXcd test_state = Eigen::VectorXcd::Zero(dim);
for (int repeat = 0; repeat < 10; repeat++) {
auto state = StateVector::Haar_random_state(n_qubits);
auto state_cp = state.amplitudes();
for (int i = 0; i < dim; i++) {
test_state[i] = state[i];
test_state[i] = state_cp[i];
}

double theta = M_PI * random.uniform();
Expand All @@ -105,11 +106,12 @@ void run_random_gate_apply(UINT n_qubits,
const UINT target = random.int64() % n_qubits;
const U3 gate(target, theta, phi, lambda);
gate.update_quantum_state(state);
state_cp = state.amplitudes();

test_state = get_expanded_eigen_matrix_with_identity(target, matrix, n_qubits) * test_state;

for (int i = 0; i < dim; i++) {
ASSERT_NEAR(std::abs((CComplex)state[i] - test_state[i]), 0, eps);
ASSERT_NEAR(std::abs((CComplex)state_cp[i] - test_state[i]), 0, eps);
}
}
}
Expand All @@ -124,6 +126,7 @@ void run_random_gate_apply_two_qubit(UINT n_qubits) {
for (int repeat = 0; repeat < 10; repeat++) {
auto state = StateVector::Haar_random_state(n_qubits);
for (int g = 0; g < 2; g++) {
auto state_cp = state.amplitudes();
for (int i = 0; i < dim; i++) {
test_state[i] = state[i];
}
Expand All @@ -139,12 +142,13 @@ void run_random_gate_apply_two_qubit(UINT n_qubits) {
func_eig = get_eigen_matrix_full_qubit_CZ;
}
gate->update_quantum_state(state);
state_cp = state.amplitudes();

Eigen::MatrixXcd test_mat = func_eig(control, target, n_qubits);
test_state = test_mat * test_state;

for (int i = 0; i < dim; i++) {
ASSERT_NEAR(std::abs(state[i] - test_state[i]), 0, eps);
ASSERT_NEAR(std::abs((CComplex)state_cp[i] - test_state[i]), 0, eps);
}
}
delete gate;
Expand All @@ -153,21 +157,23 @@ void run_random_gate_apply_two_qubit(UINT n_qubits) {
func_eig = get_eigen_matrix_full_qubit_SWAP;
for (int repeat = 0; repeat < 10; repeat++) {
auto state = StateVector::Haar_random_state(n_qubits);
auto state_cp = state.amplitudes();
for (int i = 0; i < dim; i++) {
test_state[i] = state[i];
test_state[i] = state_cp[i];
}

UINT target = random.int64() % n_qubits;
UINT control = random.int64() % n_qubits;
if (target == control) target = (target + 1) % n_qubits;
gate = new SWAP(control, target);
gate->update_quantum_state(state);
state_cp = state.amplitudes();

Eigen::MatrixXcd test_mat = func_eig(control, target, n_qubits);
test_state = test_mat * test_state;

for (int i = 0; i < dim; i++) {
ASSERT_NEAR(std::abs(state[i] - test_state[i]), 0, eps);
ASSERT_NEAR(std::abs((CComplex)state_cp[i] - test_state[i]), 0, eps);
}
}
delete gate;
Expand All @@ -185,11 +191,15 @@ void run_random_gate_apply_fused(UINT n_qubits, UINT target0, UINT target1, UINT
swap_gate->update_quantum_state(state_ref);
delete swap_gate;
}
auto state_ref_cp = state_ref.amplitudes();

swap_gate = new FusedSWAP(target0, target1, block_size);
swap_gate->update_quantum_state(state);
delete swap_gate;
auto state_cp = state.amplitudes();

for (UINT i = 0; i < dim; i++) {
ASSERT_NEAR(std::abs(state[i] - state_ref[i]), 0, eps);
ASSERT_NEAR(std::abs((CComplex)state_cp[i] - (CComplex)state_ref_cp[i]), 0, eps);
}
}

Expand Down

0 comments on commit 6c992ae

Please sign in to comment.