Skip to content

Commit 1017077

Browse files
committed
CNOT gate was incorrectly checking target qubit state. Corrected condition to only check control qubit, fixing CNOT gate behavior.
1 parent 29f4fdc commit 1017077

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

qubitverse/simulator/gates/gates.hh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ namespace simulator
193193
if ((i & (1 << q_control)) != 0)
194194
{ // If control qubit is 1
195195
std::size_t target_bit_flipped_index = i ^ (1 << q_target);
196-
std::swap(__s[i], __s[target_bit_flipped_index]);
196+
// Only swap once per pair.
197+
if (i < target_bit_flipped_index)
198+
{
199+
std::swap(__s[i], __s[target_bit_flipped_index]);
200+
}
197201
}
198202
}
199203
}
@@ -211,14 +215,14 @@ namespace simulator
211215
{
212216
for (std::size_t i = 0; i < (1 << n_qubits); ++i)
213217
{
214-
// Extract the bits at positions q1 and q2.
218+
// Extract the bits at positions q_control and q_target.
215219
std::size_t bit_q1 = (i >> q_control) & 1;
216220
std::size_t bit_q2 = (i >> q_target) & 1;
217221

218222
// Only need to swap if the bits differ.
219223
if (bit_q1 != bit_q2)
220224
{
221-
// Flip the bits at q1 and q2.
225+
// Flip the bits at q_control and q_target.
222226
std::size_t j = i ^ ((1 << q_control) | (1 << q_target));
223227
// To avoid double swapping, swap only if i < j.
224228
if (i < j)

0 commit comments

Comments
 (0)