Skip to content

Commit

Permalink
add test of probablisticgate
Browse files Browse the repository at this point in the history
  • Loading branch information
KowerKoint committed Jun 25, 2024
1 parent 74e315c commit 773cbdc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/gate/gate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,23 @@ TEST(GateTest, ApplyFused) {
}

TEST(GateTest, ApplyPauliGate) { run_random_gate_apply_pauli(5); }

TEST(GateTest, ApplyProbablisticGate) {
auto probgate = gate::Probablistic({.1, .9}, {gate::X(0), gate::I()});
UINT x_cnt = 0, i_cnt = 0;
StateVector state(1);
for (auto _ : std::views::iota(0, 100)) {
UINT before = state.sampling(1)[0];
probgate->update_quantum_state(state);
UINT after = state.sampling(1)[0];
if (before != after) {
x_cnt++;
} else {
i_cnt++;
}
}
// These test is probablistic, but pass at least 99.99% cases.
ASSERT_GT(x_cnt, 0);
ASSERT_GT(i_cnt, 0);
ASSERT_LT(x_cnt, i_cnt);
}
20 changes: 20 additions & 0 deletions tests/gate/param_gate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,23 @@ TEST(ParamGateTest, ApplyPRZGate) {
test_apply_parametric_single_pauli_rotation(5, &gate::RX, &gate::PRX);
}
TEST(ParamGateTest, ApplyPPauliRotationGate) { test_apply_parametric_multi_pauli_rotation(5); }

TEST(ParamGateTest, ApplyPProbablisticGate) {
auto probgate = gate::PProbablistic({.1, .9}, {gate::PRX(0), gate::I()});
UINT x_cnt = 0, i_cnt = 0;
StateVector state(1);
for (auto _ : std::views::iota(0, 100)) {
UINT before = state.sampling(1)[0];
probgate->update_quantum_state(state, scaluq::PI());
UINT after = state.sampling(1)[0];
if (before != after) {
x_cnt++;
} else {
i_cnt++;
}
}
// These test is probablistic, but pass at least 99.99% cases.
ASSERT_GT(x_cnt, 0);
ASSERT_GT(i_cnt, 0);
ASSERT_LT(x_cnt, i_cnt);
}

0 comments on commit 773cbdc

Please sign in to comment.