Skip to content

Fix phase flip algorithm and tests#332

Open
mudit06mah wants to merge 3 commits intoqutip:masterfrom
mudit06mah:fix/phase-flip
Open

Fix phase flip algorithm and tests#332
mudit06mah wants to merge 3 commits intoqutip:masterfrom
mudit06mah:fix/phase-flip

Conversation

@mudit06mah
Copy link
Contributor

Description

This PR corrects the implementation of the phase flip algorithm.

Related Issue

Closes #283

Changes

  • Placed SNOT gate after CNOT in encode_circuit().
  • Added SNOT gate before and after parity check for syndrome qubits.
  • Added SNOT before CNOT in decode_circuit().
  • Changed test cases accordingly.

References

https://qubit.guide/13.8-correcting-phase-flips

Copy link
Member

@BoxiLi BoxiLi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Could you also help us add type hints here?

@mudit06mah
Copy link
Contributor Author

@BoxiLi Sorry for overlooking test reproducibility! PTAL again!

@mudit06mah mudit06mah requested a review from BoxiLi March 9, 2026 18:06
@Mayank447
Copy link
Member

Mayank447 commented Mar 9, 2026

@mudit06mah sorry but you will have to either rebase this PR of gate_refactor branch or wait until this week 🤞 for gate_refactor to be merged to master.

The reason is I remember making quite some changes in bit-flip, phase-flip files because of some error etc. The testing was considerably changed due us replacing gates in QubitCircuit completely with instructions.

(SNOT) gates followed by two CNOT gates. This creates redundancy to detect and correct
a single phase error.
The logical qubit is first encoded by two CNOT gates and then converted to the X-basis
using Hadamard (SNOT). This creates redundancy to detect and correct a single phase error.
Copy link
Member

@Mayank447 Mayank447 Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using Hadamard (SNOT). This creates redundancy to detect and correct a single phase error.
using Hadamard (H). This creates redundancy to detect and correct a single phase error.

Just did a google search for SNOT gate, it gave Did you mean: NOT gate. I don't know where the term SNOT originated from, @BoxiLi any ideas?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, historical reason. I also didn't find the source. The two original developer chosed them in 2014 I think. We should encourage the use of H


It first applies the inverse of the CNOT encoding, then converts the qubits
back from the X-basis to the Z-basis using Hadamard (SNOT) gates.
It first applies the Hadamard (SNOT) gates to convert the qubits back from
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It first applies the Hadamard (SNOT) gates to convert the qubits back from
It first applies the Hadamard (H) gates to convert the qubits back from


assert fidelity > 0.99, f"Fidelity too low: {fidelity:.4f}"
for _ in range(5):
seed = np.random.randint(0, 2**31 - 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the incorrect way of defining seed, it should ideally be fixed. Also numpy recommends using rng = np.random.default_rng(seed) from numpy 1.17

fidelity = qutip.fidelity(psi, final)

assert fidelity > 0.99, f"Fidelity too low: {fidelity:.4f}"
for _ in range(5):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use @pytest.mark.repeat(5) instead.

qc.add_gate("SNOT", targets=[q])

control = data_qubits[0]
for target in reversed(data_qubits[1:]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for target in reversed(data_qubits[1:]):
for target in data_qubits[1::-1]:

@mudit06mah
Copy link
Contributor Author

@Mayank447 No worries! I'll wait untill the gate refactor branch is merged.
I think after that has been done we should also deprecate the "SNOT" gate since i couldn't find this term anywhere but in qutip documents only, There was also an issue about removing duplicate gates so I think we should just keep the "H" gate, I have currently replaced "SNOT" everywhere in my code.
Please let me know if you need help with gate refactor as well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error in the phase flip code

3 participants