Skip to content

Commit

Permalink
Fix cz in transpiler (#558)
Browse files Browse the repository at this point in the history
* experimental implementation for testing purposes

* added draft implementation

* added unit tests

* added changelog

* fix unit tests

* fix code quality check

* fix documentation

* add support for wait gates in circuit transpilation

* added documentation for wait gate in gate_decompositions

* fixed bug with None values in options

* add explanation for option method in gate_event_settings.post_init

* merge with main

* merge with main

* fix code quality complains

* fix test coverage

* fix test coverage

* fix test coverage

* add cz to data

* add 2q gate with spaces in test.data runcard to fix codecov

* add flipped cz to test_optimize_transpilation
  • Loading branch information
visagim authored Oct 10, 2023
1 parent 0d2eb76 commit 4bf73b9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 23 deletions.
23 changes: 1 addition & 22 deletions src/qililab/pulse/circuit_to_pulses.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from dataclasses import asdict

import numpy as np
from qibo.gates import CZ, Gate, M
from qibo.gates import Gate, M
from qibo.models.circuit import Circuit

from qililab.chip.nodes import Coupler, Qubit
Expand Down Expand Up @@ -90,9 +90,6 @@ def translate( # pylint: disable=too-many-locals, too-many-branches

# handle control gates
else:
# parse symmetry in CZ gates
if isinstance(gate, CZ):
gate = self._parse_check_cz(gate)
# extract gate schedule
gate_schedule = self._gate_schedule_from_settings(gate)
gate_qubits = self._get_gate_qubits(gate, gate_schedule)
Expand Down Expand Up @@ -262,24 +259,6 @@ def _gate_element_to_pulse_event(
qubit=qubit,
)

def _parse_check_cz(self, cz: CZ):
"""Checks if CZ is defined in the runcard, otherwise returns its symmetric gate (with flipped qubits)
If none of those are defined in the runcard, a KeyError will be raised by platform.settings on trying
to find the gate with qubits flipped
Args:
cz (CZ): qibo CZ gate
Returns:
CZ: qibo CZ gate
"""
cz_qubits = cz.qubits
try:
self.platform.gates_settings.get_gate(name=cz.__class__.__name__, qubits=cz_qubits)
return cz
except KeyError:
return CZ(cz_qubits[1], cz_qubits[0])

def _update_time(self, time: dict[int, int], qubit: int, gate_time: int):
"""Creates new timeline if not already created and update time.
Expand Down
5 changes: 5 additions & 0 deletions src/qililab/settings/runcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,17 @@ def get_gate(self, name: str, qubits: int | tuple[int, int] | tuple[int]):
(qubits,) if isinstance(qubits, int) else qubits
) # tuplify so that the join method below is general
gate_name = f"{name}({', '.join(map(str, gate_qubits))})"
gate_name_t = f"{name}({', '.join(map(str, gate_qubits[::-1]))})"

# parse spaces in tuple if needed, check first case with spaces since it is more common
if gate_name.replace(" ", "") in self.gates.keys():
return self.gates[gate_name.replace(" ", "")]
if gate_name in self.gates.keys():
return self.gates[gate_name]
if gate_name_t.replace(" ", "") in self.gates.keys():
return self.gates[gate_name_t.replace(" ", "")]
if gate_name_t in self.gates.keys():
return self.gates[gate_name_t]
raise KeyError(f"Gate {name} for qubits {qubits} not found in settings.")

@property
Expand Down
24 changes: 24 additions & 0 deletions tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,30 @@ class Galadriel:
},
}
],
"CZ(0,1)": [
{
"bus": "flux_line_q1_bus",
"wait_time": 0,
"pulse": {
"amplitude": 1.0,
"phase": 1.5707963267948966,
"duration": 20,
"shape": {"name": "rectangular"},
},
}
],
"CZ(0, 2)": [
{
"bus": "flux_line_q0_bus",
"wait_time": 0,
"pulse": {
"amplitude": 1.0,
"phase": 1.5707963267948966,
"duration": 20,
"shape": {"name": "rectangular"},
},
}
],
},
}

Expand Down
13 changes: 13 additions & 0 deletions tests/settings/test_runcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def test_get_operation_settings(self, gates_settings):
gates_settings.OperationSettings,
)

def test_get_parameter_fails(self, gates_settings):
with pytest.raises(ValueError, match="Could not find gate alias in gate settings."):
gates_settings.get_parameter(alias="alias", parameter=Parameter.DURATION)

def test_get_operation_settings_raises_error_when_operation_does_not_exist(self, gates_settings):
"""Test the ``get_gate`` method of the Runcard.GatesSettings class."""
name = "unkown_operation"
Expand All @@ -123,6 +127,15 @@ def test_get_gate(self, gates_settings):
for gate_event in gates_settings.get_gate(name=gate_name, qubits=ast.literal_eval(gate_qubits))
)

# check that CZs commute
# CZ(0,1) doesn't have spaces in the tuple string
assert isinstance(gates_settings.get_gate(name="CZ", qubits=(1, 0))[0], GateEventSettings)
assert isinstance(gates_settings.get_gate(name="CZ", qubits=(0, 1))[0], GateEventSettings)

# CZ(0, 2) has spaces in the tuple string
assert isinstance(gates_settings.get_gate(name="CZ", qubits=(2, 0))[0], GateEventSettings)
assert isinstance(gates_settings.get_gate(name="CZ", qubits=(0, 2))[0], GateEventSettings)

def test_get_gate_raises_error(self, gates_settings):
"""Test that the ``get_gate`` method raises an error when the name is not found."""
name = "test"
Expand Down
11 changes: 10 additions & 1 deletion tests/transpiler/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,20 @@ def test_optimize_transpilation(platform):
gates.RZ(0, 2),
Drag(0, 3, 3),
gates.CZ(0, 2),
gates.CZ(1, 0),
Drag(1, 2, 3),
gates.RZ(1, 0),
]
# resulting gate list from optimization
result_gates = [Drag(0, 1, 1), gates.CZ(0, 1), gates.M(0), Drag(0, 3, 0), gates.CZ(0, 2), Drag(1, 2, 0)]
result_gates = [
Drag(0, 1, 1),
gates.CZ(0, 1),
gates.M(0),
Drag(0, 3, 0),
gates.CZ(0, 2),
gates.CZ(1, 0),
Drag(1, 2, -2),
]

# check that lists are the same
optimized_gates = optimize_transpilation(3, test_gates, gates_settings=platform.gates_settings)
Expand Down

0 comments on commit 4bf73b9

Please sign in to comment.