Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gates.CNOT as a native gate in gate decomposition #1422

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/qibo/transpiler/decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ def _u3_to_gpi2(t, p, l):
lambda gate: two_qubit_decomposition(0, 1, gate.matrix(backend), backend=backend),
)

# temporary CNOT decompositions for CNOT, CZ, SWAP
cnot_dec_temp = GateDecompositions()
cnot_dec_temp.add(gates.CNOT, [gates.CNOT(0, 1)])
cnot_dec_temp.add(gates.CZ, [gates.H(1), gates.CNOT(0, 1), gates.H(1)])
cnot_dec_temp.add(gates.SWAP, [gates.CNOT(0, 1), gates.CNOT(1, 0), gates.CNOT(0, 1)])

# register other optimized gate decompositions
opt_dec = GateDecompositions()
opt_dec.add(
Expand Down
20 changes: 18 additions & 2 deletions src/qibo/transpiler/unroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
from qibo.config import raise_error
from qibo.models import Circuit
from qibo.transpiler._exceptions import DecompositionError
from qibo.transpiler.decompositions import cz_dec, gpi2_dec, iswap_dec, opt_dec, u3_dec
from qibo.transpiler.decompositions import (
cnot_dec_temp,
cz_dec,
gpi2_dec,
iswap_dec,
opt_dec,
u3_dec,
)


class NativeGates(Flag):
Expand All @@ -22,6 +29,7 @@ class NativeGates(Flag):
- :class:`qibo.gates.gates.U3`
- :class:`qibo.gates.gates.CZ`
- :class:`qibo.gates.gates.iSWAP`
- :class:`qibo.gates.gates.CNOT`
"""

I = auto()
Expand All @@ -32,6 +40,7 @@ class NativeGates(Flag):
U3 = auto()
CZ = auto()
iSWAP = auto()
CNOT = auto() # For testing purposes
alecandido marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def default(cls):
Expand Down Expand Up @@ -240,6 +249,13 @@ def _translate_two_qubit_gates(gate: gates.Gate, native_gates: NativeGates):
iswap_decomposed.append(g_translated)
return iswap_decomposed

# For testing purposes
# No CZ, iSWAP gates in the native gate set
# Decompose CNOT, CZ, SWAP gates into CNOT gates
if native_gates & NativeGates.CNOT:
return cnot_dec_temp(gate)
Copy link
Contributor

Choose a reason for hiding this comment

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

You can make this decomposition work for every gate by first decomposing into CZ and then decomposing CZ into CNOT. This is how it works for iSWAP decomposition (line 238-250).

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but it adds two H gates next to the CNOT and it requires optimization (fusion) of 1q gates. Since Paul will use simple circuits for testing, I only added basic decompositions. As @alecandido mentioned, it would be better to entirely review the unroller.

Copy link
Contributor

Choose a reason for hiding this comment

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

It should be a realy easy check to remove two successive H gates, however if you need it only for simple applications it is fine


raise_error(
DecompositionError, "Use only CZ and/or iSWAP as native gates"
DecompositionError,
"Use only CZ and/or iSWAP as native gates. CNOT is allowed in circuits where the two-qubit gates are limited to CZ, CNOT, and SWAP.",
) # pragma: no cover
33 changes: 33 additions & 0 deletions tests/test_transpiler_unroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,36 @@ def test_measurements_non_comp_basis():
assert isinstance(transpiled_circuit.queue[2], gates.M)
# After transpiling the measurement gate should be in the computational basis
assert transpiled_circuit.queue[2].basis == []


def test_temp_cnot_decomposition():
from qibo.transpiler.pipeline import Passes

circ = Circuit(2)
circ.add(gates.H(0))
circ.add(gates.CNOT(0, 1))
circ.add(gates.SWAP(0, 1))
circ.add(gates.CZ(0, 1))
circ.add(gates.M(0, 1))

glist = [gates.GPI2, gates.RZ, gates.Z, gates.M, gates.CNOT]
native_gates = NativeGates(0).from_gatelist(glist)

custom_pipeline = Passes([Unroller(native_gates=native_gates)])
transpiled_circuit, _ = custom_pipeline(circ)

# H
assert transpiled_circuit.queue[0].name == "z"
assert transpiled_circuit.queue[1].name == "gpi2"
# CNOT
assert transpiled_circuit.queue[2].name == "cx"
# SWAP
assert transpiled_circuit.queue[3].name == "cx"
assert transpiled_circuit.queue[4].name == "cx"
assert transpiled_circuit.queue[5].name == "cx"
# CZ
assert transpiled_circuit.queue[6].name == "z"
assert transpiled_circuit.queue[7].name == "gpi2"
assert transpiled_circuit.queue[8].name == "cx"
assert transpiled_circuit.queue[9].name == "z"
assert transpiled_circuit.queue[10].name == "gpi2"