TeamGraphix / graphix

measurement-based quantum computing (MBQC) compiler and simulator
https://graphix.readthedocs.io
Apache License 2.0
60 stars 21 forks source link

Measurement pattern for Toffoli gate #49

Closed shinich1 closed 9 months ago

shinich1 commented 1 year ago

Implement measurement pattern for Toffoli gate in graphix.Circuit class for transpilation. Please consider:

Please see our contribution guide before starting, and do not forget to comment on this issue to show your interest. We are always happy to answer questions, so do not hesitate to ask on this page.

Thank you for your interest, and good luck!

king-p3nguin commented 1 year ago

Hi @shinich1, I'm interested in working on this issue as part of the Unitary Hack. Can I be assigned to this challenge? Thank you!

shinich1 commented 1 year ago

Hi @shinich1, I'm interested in working on this issue as part of the Unitary Hack. Can I be assigned to this challenge?

Thank you!

Yes please go ahead and work on this issue, thanks for your interest! According to unitaryHack guide, assigning to issue indicates successful merging of associated PR - so it should wait until there's a PR ready to merge.

Feel free to ask any question regarding graphix or the issue on this page :)

king-p3nguin commented 1 year ago

Oh, I misunderstood that I have to be assigned prior to the PR. Thank you for the correction.

king-p3nguin commented 1 year ago

Hi, I have a question. I used a textbook method to decompose Toffili gate as follows, and it seems like running pattern.perform_pauli_measurements() will give a wrong result $\ket{111}$ instead of $\ket{110}$. Am I missing out on something?

circuit = Circuit(3)
# prepare |000>
circuit.h(0)
circuit.h(1)
circuit.h(2)
# prepare |111>
circuit.x(0)
circuit.x(1)
circuit.x(2)
# toffoli
circuit.h(2)
circuit.cnot(1, 2)
circuit.rz(2, -np.pi/4)
circuit.cnot(0, 2)
circuit.rz(2, np.pi/4)
circuit.cnot(1, 2)
circuit.rz(1, np.pi/4)
circuit.rz(2, -np.pi/4)
circuit.cnot(0, 2)
circuit.cnot(0, 1)
circuit.rz(2, np.pi/4)
circuit.rz(0, np.pi/4)
circuit.rz(1, -np.pi/4)
circuit.h(2)
circuit.cnot(0, 1)
# transpile
pattern = circuit.transpile()
pattern.standardize()
pattern.shift_signals()
# pattern.perform_pauli_measurements() # <- Running this will break the rule of Toffoli gate?
pattern.minimize_space()
# draw with latex
from qiskit.quantum_info import Statevector
ket = pattern.simulate_pattern(backend='statevector').flatten()
ket = Statevector(ket)
ket.draw(output='latex')
shinich1 commented 1 year ago

@king-p3nguin thanks for your question - could you let us know the reference for this Toffoli decomposition? either way it's strange that the Pauli measurement preprocessing change the outcome of the computation; I will look into it and respond over the weekend.

king-p3nguin commented 1 year ago

Thank you for investigating! I used the Toffoli decomposition method implemented in qiskit.

from qiskit import QuantumCircuit
qc = QuantumCircuit(3)
qc.ccx(0, 1, 2)
qc.decompose(reps=1).draw()

image

shinich1 commented 1 year ago

@king-p3nguin please try again after incorporating (merge/rebase) 5a47704 to your forked repo.

king-p3nguin commented 1 year ago

Thank you for the bug fix! pattern.perform_pauli_measurements() seems to be working correctly now.