Skip to content

Commit

Permalink
Simplify controlled gateset (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
speller26 authored Mar 6, 2024
1 parent e0f89d0 commit ab38507
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 31 deletions.
40 changes: 10 additions & 30 deletions qiskit_braket_provider/providers/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import warnings
from collections.abc import Callable, Iterable
from math import pi
from math import inf, pi
from typing import Optional, Union

import braket.circuits.gates as braket_gates
Expand Down Expand Up @@ -80,8 +80,8 @@
"ccz": "cz",
},
3: {"c3sx": "sx"},
inf: {"mcx": "cx"},
}
_ARBITRARY_CONTROLLED_GATES = {"mcx": "cx"}

_ADDITIONAL_U_GATES = {"u1", "u2", "u3"}

Expand Down Expand Up @@ -206,8 +206,7 @@ def gateset_from_properties(properties: OpenQASMDeviceActionProperties) -> set[s
if isinstance(modifier, Control):
max_control = modifier.max_qubits
break
gateset.update(_get_controlled_gateset(gateset, max_control))
return gateset
return gateset.union(_get_controlled_gateset(gateset, max_control))


def _get_controlled_gateset(
Expand All @@ -226,32 +225,13 @@ def _get_controlled_gateset(
Returns:
set[str]: The names of the controlled gates.
"""
if max_qubits is None:
gateset = set().union(
[
controlled_gate
for gate_map in _CONTROLLED_GATES_BY_QUBIT_COUNT.values()
for controlled_gate, base_gate in gate_map.items()
if base_gate in base_gateset
]
)
gateset.update(
[
controlled_gate
for controlled_gate, base_gate in _ARBITRARY_CONTROLLED_GATES.items()
if base_gate in base_gateset
]
)
gateset.update(_ARBITRARY_CONTROLLED_GATES)
return gateset
return set().union(
[
controlled_gate
for control_count, gate_map in _CONTROLLED_GATES_BY_QUBIT_COUNT.items()
for controlled_gate, base_gate in gate_map.items()
if control_count <= max_qubits and base_gate in base_gateset
]
)
max_control = max_qubits if max_qubits is not None else inf
return {
controlled_gate
for control_count, gate_map in _CONTROLLED_GATES_BY_QUBIT_COUNT.items()
for controlled_gate, base_gate in gate_map.items()
if control_count <= max_control and base_gate in base_gateset
}


def local_simulator_to_target(simulator: LocalSimulator) -> Target:
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def test_invalid_ctrl_state(self, mock_transpile):

def test_get_controlled_gateset(self):
"""Tests that the correct controlled gateset is returned for all maximum qubit counts."""
full_gateset = {"h", "s", "sdg", "sx", "rx", "ry", "rz", "cz"}
full_gateset = {"h", "s", "sdg", "sx", "rx", "ry", "rz", "cx", "cz"}
restricted_gateset = {"rx", "cx", "sx"}
max1 = {"ch", "cs", "csdg", "csx", "crx", "cry", "crz", "ccz"}
max3 = max1.union({"c3sx"})
Expand Down

0 comments on commit ab38507

Please sign in to comment.