Skip to content

Commit

Permalink
feat: add connectivity to transpile (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
shpface authored Aug 14, 2024
1 parent 2b09a3f commit 3f87f78
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
17 changes: 13 additions & 4 deletions qiskit_braket_provider/providers/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ def to_braket(
circuit: QuantumCircuit,
basis_gates: Optional[Iterable[str]] = None,
verbatim: bool = False,
connectivity: Optional[list[list[int]]] = None,
) -> Circuit:
"""Return a Braket quantum circuit from a Qiskit quantum circuit.
Expand All @@ -419,6 +420,8 @@ def to_braket(
Default: `None`.
verbatim (bool): Whether to translate the circuit without any modification, in other
words without transpiling it. Default: False.
connectivity (Optional[list[list[int]]): If provided, will transpile to a circuit
with this connectivity. Default: `None`.
Returns:
Circuit: Braket circuit
Expand All @@ -429,10 +432,16 @@ def to_braket(
basis_gates = set(basis_gates or _TRANSLATABLE_QISKIT_GATE_NAMES)

braket_circuit = Circuit()
if not verbatim and not {gate.name for gate, _, _ in circuit.data}.issubset(
basis_gates
):
circuit = transpile(circuit, basis_gates=basis_gates, optimization_level=0)
needs_transpilation = connectivity or not {
gate.name for gate, _, _ in circuit.data
}.issubset(basis_gates)
if not verbatim and needs_transpilation:
circuit = transpile(
circuit,
basis_gates=basis_gates,
coupling_map=connectivity,
optimization_level=0,
)

# Verify that ParameterVector would not collide with scalar variables after renaming.
_validate_name_conflicts(circuit.parameters)
Expand Down
38 changes: 38 additions & 0 deletions tests/providers/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,44 @@ def test_get_controlled_gateset(self):
"mcx",
}

def test_connectivity(self):
"""Tests transpiling with connectivity"""
qiskit_circuit = QuantumCircuit(3)
qiskit_circuit.h(0)
qiskit_circuit.cx(0, 1)
qiskit_circuit.rxx(0.1, 0, 2)
connectivity = [[0, 1], [1, 0], [1, 2], [2, 1]]

braket_circuit = to_braket(qiskit_circuit, connectivity=connectivity)
braket_circuit_unconnected = to_braket(qiskit_circuit)
braket_circuit_verbatim = to_braket(
qiskit_circuit, verbatim=True, connectivity=connectivity
)

def gate_matches_connectivity(gate) -> bool:
return any(
(
gate.target.union(gate.control).issubset(adjacency)
for adjacency in connectivity
)
)

assert all(
(gate_matches_connectivity(gate) for gate in braket_circuit.instructions)
)
assert not all(
(
gate_matches_connectivity(gate)
for gate in braket_circuit_unconnected.instructions
)
)
assert not all(
(
gate_matches_connectivity(gate)
for gate in braket_circuit_verbatim.instructions
)
)


class TestFromBraket(TestCase):
"""Test Braket circuit conversion."""
Expand Down

0 comments on commit 3f87f78

Please sign in to comment.