Skip to content

Commit

Permalink
Refactor stim::VectorSimulator to take GateType enums instead of …
Browse files Browse the repository at this point in the history
…gate names (#668)

Not really that important... more a point of principle. Just less wasteful.
  • Loading branch information
Strilanc authored Nov 25, 2023
1 parent 39c0217 commit 50d4db5
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/stim/simulators/tableau_simulator.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ std::vector<std::complex<float>> TableauSimulator<W>::to_state_vector(bool littl
auto sim = to_vector_sim();
if (!little_endian && inv_state.num_qubits > 0) {
for (size_t q = 0; q < inv_state.num_qubits - q - 1; q++) {
sim.apply("SWAP", q, inv_state.num_qubits - q - 1);
sim.apply(GateType::SWAP, q, inv_state.num_qubits - q - 1);
}
}
return sim.state;
Expand Down
14 changes: 7 additions & 7 deletions src/stim/simulators/tableau_simulator.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,27 +391,27 @@ TEST_EACH_WORD_SIZE_W(TableauSimulator, to_vector_sim, {
ASSERT_TRUE(sim_tab.to_vector_sim().approximate_equals(sim_vec, true));

sim_tab.do_X(OpDat(0));
sim_vec.apply("X", 0);
sim_vec.apply(GateType::X, 0);
ASSERT_TRUE(sim_tab.to_vector_sim().approximate_equals(sim_vec, true));

sim_tab.do_H_XZ(OpDat(0));
sim_vec.apply("H_XZ", 0);
sim_vec.apply(GateType::H, 0);
ASSERT_TRUE(sim_tab.to_vector_sim().approximate_equals(sim_vec, true));

sim_tab.do_SQRT_Z(OpDat(0));
sim_vec.apply("SQRT_Z", 0);
sim_vec.apply(GateType::S, 0);
ASSERT_TRUE(sim_tab.to_vector_sim().approximate_equals(sim_vec, true));

sim_tab.do_ZCX(OpDat({0, 1}));
sim_vec.apply("ZCX", 0, 1);
sim_vec.apply(GateType::CX, 0, 1);
ASSERT_TRUE(sim_tab.to_vector_sim().approximate_equals(sim_vec, true));

sim_tab.inv_state = Tableau<W>::random(10, rng);
sim_vec = sim_tab.to_vector_sim();
ASSERT_TRUE(sim_tab.to_vector_sim().approximate_equals(sim_vec, true));

sim_tab.do_gate({GateType::XCX, {}, qubit_targets({4, 7})});
sim_vec.apply("XCX", 4, 7);
sim_vec.apply(GateType::XCX, 4, 7);
ASSERT_TRUE(sim_tab.to_vector_sim().approximate_equals(sim_vec, true));
})

Expand All @@ -432,8 +432,8 @@ TEST_EACH_WORD_SIZE_W(TableauSimulator, to_state_vector, {
TEST_EACH_WORD_SIZE_W(TableauSimulator, to_state_vector_endian, {
VectorSimulator sim_vec0(3);
VectorSimulator sim_vec2(3);
sim_vec0.apply("H", 0);
sim_vec2.apply("H", 2);
sim_vec0.apply(GateType::H, 0);
sim_vec2.apply(GateType::H, 2);

TableauSimulator<W> sim_tab(INDEPENDENT_TEST_RNG(), 3);
sim_tab.do_H_XZ(OpDat(2));
Expand Down
14 changes: 8 additions & 6 deletions src/stim/simulators/vector_simulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,21 @@ void VectorSimulator::apply(
}
}

void VectorSimulator::apply(const std::string &gate, size_t qubit) {
void VectorSimulator::apply(GateType gate, size_t qubit) {
try {
apply(GATE_DATA.at(gate).unitary(), {qubit});
apply(GATE_DATA[gate].unitary(), {qubit});
} catch (const std::out_of_range &) {
throw std::out_of_range("Single qubit gate isn't supported by VectorSimulator: " + gate);
throw std::out_of_range(
"Single qubit gate isn't supported by VectorSimulator: " + std::string(GATE_DATA[gate].name));
}
}

void VectorSimulator::apply(const std::string &gate, size_t qubit1, size_t qubit2) {
void VectorSimulator::apply(GateType gate, size_t qubit1, size_t qubit2) {
try {
apply(GATE_DATA.at(gate).unitary(), {qubit1, qubit2});
apply(GATE_DATA[gate].unitary(), {qubit1, qubit2});
} catch (const std::out_of_range &) {
throw std::out_of_range("Two qubit gate isn't supported by VectorSimulator: " + gate);
throw std::out_of_range(
"Two qubit gate isn't supported by VectorSimulator: " + std::string(GATE_DATA[gate].name));
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/stim/simulators/vector_simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ struct VectorSimulator {
void apply(const std::vector<std::vector<std::complex<float>>> &matrix, const std::vector<size_t> &qubits);

/// Helper method for applying named single qubit gates.
void apply(const std::string &gate, size_t qubit);
void apply(GateType gate, size_t qubit);
/// Helper method for applying named two qubit gates.
void apply(const std::string &gate, size_t qubit1, size_t qubit2);
void apply(GateType gate, size_t qubit1, size_t qubit2);

/// Helper method for applying the gates in a Pauli string.
template <size_t W>
Expand All @@ -102,11 +102,11 @@ struct VectorSimulator {
bool z = gate.zs[k];
size_t q = qubit_offset + k;
if (x && z) {
apply("Y", q);
apply(GateType::Y, q);
} else if (x) {
apply("X", q);
apply(GateType::X, q);
} else if (z) {
apply("Z", q);
apply(GateType::Z, q);
}
}
}
Expand All @@ -132,9 +132,9 @@ struct VectorSimulator {
for (size_t k = 0; k < observable.num_qubits; k++) {
if (observable.xs[k]) {
if (observable.zs[k]) {
apply("H_YZ", k);
apply(GateType::H_YZ, k);
} else {
apply("H_XZ", k);
apply(GateType::H, k);
}
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/stim/simulators/vector_simulator.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ static float complex_distance(std::complex<float> a, std::complex<float> b) {

TEST(vector_sim, qubit_order) {
VectorSimulator sim(2);
sim.apply("H_XZ", 0);
sim.apply("ZCX", 0, 1);
sim.apply(GateType::H, 0);
sim.apply(GateType::CX, 0, 1);
ASSERT_NEAR_C(sim.state[0], sqrtf(0.5));
ASSERT_NEAR_C(sim.state[1], 0);
ASSERT_NEAR_C(sim.state[2], 0);
Expand All @@ -41,27 +41,27 @@ TEST(vector_sim, qubit_order) {

TEST(vector_sim, h_squared) {
VectorSimulator sim(1);
sim.apply("H_XZ", 0);
sim.apply("H_XZ", 0);
sim.apply(GateType::H, 0);
sim.apply(GateType::H, 0);
ASSERT_NEAR_C(sim.state[0], 1);
ASSERT_NEAR_C(sim.state[1], 0);
}

TEST(vector_sim, sqrt_x_squared) {
VectorSimulator sim(1);
sim.apply("SQRT_X_DAG", 0);
sim.apply("SQRT_X_DAG", 0);
sim.apply(GateType::SQRT_X_DAG, 0);
sim.apply(GateType::SQRT_X_DAG, 0);
ASSERT_NEAR_C(sim.state[0], 0);
ASSERT_NEAR_C(sim.state[1], 1);
}

TEST(vector_sim, state_channel_duality_cnot) {
VectorSimulator sim(4);
sim.apply("H_XZ", 0);
sim.apply("H_XZ", 1);
sim.apply("ZCX", 0, 2);
sim.apply("ZCX", 1, 3);
sim.apply("ZCX", 2, 3);
sim.apply(GateType::H, 0);
sim.apply(GateType::H, 1);
sim.apply(GateType::CX, 0, 2);
sim.apply(GateType::CX, 1, 3);
sim.apply(GateType::CX, 2, 3);
auto u = GATE_DATA.at("ZCX").unitary();
for (size_t row = 0; row < 4; row++) {
for (size_t col = 0; col < 4; col++) {
Expand All @@ -72,9 +72,9 @@ TEST(vector_sim, state_channel_duality_cnot) {

TEST(vector_sim, state_channel_duality_y) {
VectorSimulator sim(2);
sim.apply("H_XZ", 0);
sim.apply("ZCX", 0, 1);
sim.apply("Y", 1);
sim.apply(GateType::H, 0);
sim.apply(GateType::CX, 0, 1);
sim.apply(GateType::Y, 1);
auto u = GATE_DATA.at("Y").unitary();
for (size_t row = 0; row < 2; row++) {
for (size_t col = 0; col < 2; col++) {
Expand Down
6 changes: 4 additions & 2 deletions src/stim/stabilizers/conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,16 @@ bool stim::try_disjoint_to_independent_xyz_errors_approx(

double stim::depolarize1_probability_to_independent_per_channel_probability(double p) {
if (p > 0.75) {
throw std::invalid_argument("depolarize1_probability_to_independent_per_channel_probability with p>0.75; p=" + std::to_string(p));
throw std::invalid_argument(
"depolarize1_probability_to_independent_per_channel_probability with p>0.75; p=" + std::to_string(p));
}
return 0.5 - 0.5 * sqrt(1 - (4 * p) / 3);
}

double stim::depolarize2_probability_to_independent_per_channel_probability(double p) {
if (p > 0.9375) {
throw std::invalid_argument("depolarize2_probability_to_independent_per_channel_probability with p>15.0/16.0; p=" + std::to_string(p));
throw std::invalid_argument(
"depolarize2_probability_to_independent_per_channel_probability with p>15.0/16.0; p=" + std::to_string(p));
}
return 0.5 - 0.5 * pow(1 - (16 * p) / 15, 0.125);
}
Expand Down
Loading

0 comments on commit 50d4db5

Please sign in to comment.