|
| 1 | +// Copyright 2025 The PECOS Developers |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 4 | +// in compliance with the License.You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software distributed under the License |
| 9 | +// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 10 | +// or implied. See the License for the specific language governing permissions and limitations under |
| 11 | +// the License. |
| 12 | + |
| 13 | +#include "cxx_shim.h" |
| 14 | + |
| 15 | +StateWrapper::StateWrapper(std::uint64_t num_qubits, std::int32_t reserve_buckets) |
| 16 | + : state(static_cast<int_num>(num_qubits), static_cast<int>(reserve_buckets)) {} |
| 17 | + |
| 18 | +void StateWrapper::set_seed(std::uint32_t seed) { |
| 19 | + // Set the instance RNG seed |
| 20 | + state.set_seed(static_cast<unsigned int>(seed)); |
| 21 | +} |
| 22 | + |
| 23 | +void StateWrapper::clear() { |
| 24 | + state.clear(); |
| 25 | +} |
| 26 | + |
| 27 | +void StateWrapper::hadamard(std::uint64_t qubit) { |
| 28 | + state.hadamard(static_cast<int_num>(qubit)); |
| 29 | +} |
| 30 | + |
| 31 | +void StateWrapper::bitflip(std::uint64_t qubit) { |
| 32 | + state.bitflip(static_cast<int_num>(qubit)); |
| 33 | +} |
| 34 | + |
| 35 | +void StateWrapper::phaseflip(std::uint64_t qubit) { |
| 36 | + state.phaseflip(static_cast<int_num>(qubit)); |
| 37 | +} |
| 38 | + |
| 39 | +void StateWrapper::Y(std::uint64_t qubit) { |
| 40 | + state.Y(static_cast<int_num>(qubit)); |
| 41 | +} |
| 42 | + |
| 43 | +void StateWrapper::phaserot(std::uint64_t qubit) { |
| 44 | + state.phaserot(static_cast<int_num>(qubit)); |
| 45 | +} |
| 46 | + |
| 47 | +void StateWrapper::SZdg(std::uint64_t qubit) { |
| 48 | + state.SZdg(static_cast<int_num>(qubit)); |
| 49 | +} |
| 50 | + |
| 51 | +void StateWrapper::SY(std::uint64_t qubit) { |
| 52 | + state.SY(static_cast<int_num>(qubit)); |
| 53 | +} |
| 54 | + |
| 55 | +void StateWrapper::SYdg(std::uint64_t qubit) { |
| 56 | + state.SYdg(static_cast<int_num>(qubit)); |
| 57 | +} |
| 58 | + |
| 59 | +void StateWrapper::SX(std::uint64_t qubit) { |
| 60 | + state.SX(static_cast<int_num>(qubit)); |
| 61 | +} |
| 62 | + |
| 63 | +void StateWrapper::SXdg(std::uint64_t qubit) { |
| 64 | + state.SXdg(static_cast<int_num>(qubit)); |
| 65 | +} |
| 66 | + |
| 67 | +void StateWrapper::H2(std::uint64_t qubit) { |
| 68 | + state.H2(static_cast<int_num>(qubit)); |
| 69 | +} |
| 70 | + |
| 71 | +void StateWrapper::H3(std::uint64_t qubit) { |
| 72 | + state.H3(static_cast<int_num>(qubit)); |
| 73 | +} |
| 74 | + |
| 75 | +void StateWrapper::H4(std::uint64_t qubit) { |
| 76 | + state.H4(static_cast<int_num>(qubit)); |
| 77 | +} |
| 78 | + |
| 79 | +void StateWrapper::H5(std::uint64_t qubit) { |
| 80 | + state.H5(static_cast<int_num>(qubit)); |
| 81 | +} |
| 82 | + |
| 83 | +void StateWrapper::H6(std::uint64_t qubit) { |
| 84 | + state.H6(static_cast<int_num>(qubit)); |
| 85 | +} |
| 86 | + |
| 87 | +void StateWrapper::F(std::uint64_t qubit) { |
| 88 | + state.F(static_cast<int_num>(qubit)); |
| 89 | +} |
| 90 | + |
| 91 | +void StateWrapper::F2(std::uint64_t qubit) { |
| 92 | + state.F2(static_cast<int_num>(qubit)); |
| 93 | +} |
| 94 | + |
| 95 | +void StateWrapper::F3(std::uint64_t qubit) { |
| 96 | + state.F3(static_cast<int_num>(qubit)); |
| 97 | +} |
| 98 | + |
| 99 | +void StateWrapper::F4(std::uint64_t qubit) { |
| 100 | + state.F4(static_cast<int_num>(qubit)); |
| 101 | +} |
| 102 | + |
| 103 | +void StateWrapper::Fdg(std::uint64_t qubit) { |
| 104 | + state.Fdg(static_cast<int_num>(qubit)); |
| 105 | +} |
| 106 | + |
| 107 | +void StateWrapper::F2dg(std::uint64_t qubit) { |
| 108 | + state.F2dg(static_cast<int_num>(qubit)); |
| 109 | +} |
| 110 | + |
| 111 | +void StateWrapper::F3dg(std::uint64_t qubit) { |
| 112 | + state.F3dg(static_cast<int_num>(qubit)); |
| 113 | +} |
| 114 | + |
| 115 | +void StateWrapper::F4dg(std::uint64_t qubit) { |
| 116 | + state.F4dg(static_cast<int_num>(qubit)); |
| 117 | +} |
| 118 | + |
| 119 | +void StateWrapper::cx(std::uint64_t control, std::uint64_t target) { |
| 120 | + // The C++ cx function uses confusing parameter names but actually expects (control, target) |
| 121 | + state.cx(static_cast<int_num>(control), static_cast<int_num>(target)); |
| 122 | +} |
| 123 | + |
| 124 | +void StateWrapper::cy(std::uint64_t control, std::uint64_t target) { |
| 125 | + // CY = (I ⊗ SYdg) CX (I ⊗ SY) |
| 126 | + state.SYdg(static_cast<int_num>(target)); |
| 127 | + state.cx(static_cast<int_num>(control), static_cast<int_num>(target)); |
| 128 | + state.SY(static_cast<int_num>(target)); |
| 129 | +} |
| 130 | + |
| 131 | +void StateWrapper::cz(std::uint64_t qubit1, std::uint64_t qubit2) { |
| 132 | + // CZ = H(qubit2) CX(qubit1, qubit2) H(qubit2) |
| 133 | + state.hadamard(static_cast<int_num>(qubit2)); |
| 134 | + state.cx(static_cast<int_num>(qubit1), static_cast<int_num>(qubit2)); |
| 135 | + state.hadamard(static_cast<int_num>(qubit2)); |
| 136 | +} |
| 137 | + |
| 138 | +void StateWrapper::swap(std::uint64_t qubit1, std::uint64_t qubit2) { |
| 139 | + state.swap(static_cast<int_num>(qubit1), static_cast<int_num>(qubit2)); |
| 140 | +} |
| 141 | + |
| 142 | +void StateWrapper::g2(std::uint64_t qubit1, std::uint64_t qubit2) { |
| 143 | + // G2 gate decomposition: H(q1), CX(q2, q1), CX(q1, q2), H(q2) |
| 144 | + state.hadamard(static_cast<int_num>(qubit1)); |
| 145 | + state.cx(static_cast<int_num>(qubit2), static_cast<int_num>(qubit1)); |
| 146 | + state.cx(static_cast<int_num>(qubit1), static_cast<int_num>(qubit2)); |
| 147 | + state.hadamard(static_cast<int_num>(qubit2)); |
| 148 | +} |
| 149 | + |
| 150 | +void StateWrapper::sxx(std::uint64_t qubit1, std::uint64_t qubit2) { |
| 151 | + // SXX = SX(q1).SX(q2).SYdg(q1).CX(q1, q2).SY(q1) |
| 152 | + state.SX(static_cast<int_num>(qubit1)); |
| 153 | + state.SX(static_cast<int_num>(qubit2)); |
| 154 | + state.SYdg(static_cast<int_num>(qubit1)); |
| 155 | + state.cx(static_cast<int_num>(qubit1), static_cast<int_num>(qubit2)); |
| 156 | + state.SY(static_cast<int_num>(qubit1)); |
| 157 | +} |
| 158 | + |
| 159 | +void StateWrapper::sxxdg(std::uint64_t qubit1, std::uint64_t qubit2) { |
| 160 | + // SXXdg = X(q1).X(q2).SXX(q1, q2) |
| 161 | + state.bitflip(static_cast<int_num>(qubit1)); |
| 162 | + state.bitflip(static_cast<int_num>(qubit2)); |
| 163 | + sxx(qubit1, qubit2); // Call the wrapper's sxx implementation |
| 164 | +} |
| 165 | + |
| 166 | +std::uint32_t StateWrapper::measure(std::uint64_t qubit, std::int32_t forced_outcome, bool collapse) { |
| 167 | + // Simple wrapper - just return the measurement outcome |
| 168 | + unsigned int outcome = state.measure(static_cast<int_num>(qubit), static_cast<int>(forced_outcome), collapse); |
| 169 | + return static_cast<std::uint32_t>(outcome); |
| 170 | +} |
| 171 | + |
| 172 | +std::uint64_t StateWrapper::get_num_qubits() const { |
| 173 | + return static_cast<std::uint64_t>(state.num_qubits); |
| 174 | +} |
| 175 | + |
| 176 | +bool StateWrapper::has_stab_x(std::uint64_t gen_id, std::uint64_t qubit) const { |
| 177 | + const auto& row_set = state.stabs.row_x[static_cast<int_num>(gen_id)]; |
| 178 | + return row_set.count(static_cast<int_num>(qubit)) > 0; |
| 179 | +} |
| 180 | + |
| 181 | +bool StateWrapper::has_stab_z(std::uint64_t gen_id, std::uint64_t qubit) const { |
| 182 | + const auto& row_set = state.stabs.row_z[static_cast<int_num>(gen_id)]; |
| 183 | + return row_set.count(static_cast<int_num>(qubit)) > 0; |
| 184 | +} |
| 185 | + |
| 186 | +bool StateWrapper::has_destab_x(std::uint64_t gen_id, std::uint64_t qubit) const { |
| 187 | + const auto& row_set = state.destabs.row_x[static_cast<int_num>(gen_id)]; |
| 188 | + return row_set.count(static_cast<int_num>(qubit)) > 0; |
| 189 | +} |
| 190 | + |
| 191 | +bool StateWrapper::has_destab_z(std::uint64_t gen_id, std::uint64_t qubit) const { |
| 192 | + const auto& row_set = state.destabs.row_z[static_cast<int_num>(gen_id)]; |
| 193 | + return row_set.count(static_cast<int_num>(qubit)) > 0; |
| 194 | +} |
| 195 | + |
| 196 | +bool StateWrapper::get_sign_minus(std::uint64_t gen_id) const { |
| 197 | + return state.signs_minus.count(static_cast<int_num>(gen_id)) > 0; |
| 198 | +} |
| 199 | + |
| 200 | +bool StateWrapper::get_sign_i(std::uint64_t gen_id) const { |
| 201 | + return state.signs_i.count(static_cast<int_num>(gen_id)) > 0; |
| 202 | +} |
| 203 | + |
| 204 | +// Factory function |
| 205 | +std::unique_ptr<StateWrapper> create_state_wrapper(std::uint64_t num_qubits, std::int32_t reserve_buckets) { |
| 206 | + return std::make_unique<StateWrapper>(num_qubits, reserve_buckets); |
| 207 | +} |
0 commit comments