Skip to content

Commit

Permalink
add optimisation level 3, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdilkes committed Nov 21, 2024
1 parent e23cbfd commit 3ef56a6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
11 changes: 11 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,36 +94,47 @@ The passes applied by different levels of optimisation are specified in the tabl
* - optimisation_level = 0
- optimisation_level = 1
- optimisation_level = 2 [1]
- optimisation_level = 3
* - [DecomposeBoxes](inv:#*.passes.DecomposeBoxes)
- [DecomposeBoxes](inv:#*.passes.DecomposeBoxes)
- [DecomposeBoxes](inv:#*.passes.DecomposeBoxes)
- [DecomposeBoxes](inv:#*.passes.DecomposeBoxes)
* - [AutoRebase [2]](inv:#*.AutoRebase)
- [SynthesiseTket](inv:#*.SynthesiseTket)
- [FullPeepholeOptimise [3]](inv:#*.passes.FullPeepholeOptimise)
- [RemoveBarriers](inv:#*.passes.RemoveBarriers)
* - [FlattenRelabelRegistersPass](inv:#*.FlattenRelabelRegistersPass)
- [NormaliseTK2 [5]](inv:#*.passes.NormaliseTK2)
- [NormaliseTK2 [5]](inv:#*.passes.NormaliseTK2)
- [GreedyPauliSimp](inv:#*.passes.GreedyPauliSimp)
* -
- [DecomposeTK2 [5]](inv:#*.passes.DecomposeTK2)
- [DecomposeTK2 [5]](inv:#*.passes.DecomposeTK2)
- [NormaliseTK2 [5]](inv:#*.passes.NormaliseTK2)
* -
- [AutoRebase [2]](inv:#*.AutoRebase)
- [AutoRebase [2]](inv:#*.AutoRebase)
- [DecomposeTK2 [5]](inv:#*.passes.DecomposeTK2)
* -
- [ZZPhaseToRz](inv:#*.passes.ZZPhaseToRz)
- [RemoveRedundancies](inv:#*.passes.RemoveRedundancies)
- [AutoRebase [2]](inv:#*.AutoRebase)
* -
- [RemoveRedundancies](inv:#*.passes.RemoveRedundancies)
- [AutoSquash [4]](inv:#*.AutoSquash)
- [RemoveRedundancies](inv:#*.passes.RemoveRedundancies)
* -
- [AutoSquash [4]](inv:#*.AutoSquash)
- [FlattenRelabelRegistersPass](inv:#*.FlattenRelabelRegistersPass)
- [AutoSquash [4]](inv:#*.AutoSquash)
* -
- [FlattenRelabelRegistersPass](inv:#*.FlattenRelabelRegistersPass)
- [RemoveRedundancies](inv:#*.passes.RemoveRedundancies)
- [FlattenRelabelRegistersPass](inv:#*.FlattenRelabelRegistersPass)
* -
- [RemoveRedundancies](inv:#*.passes.RemoveRedundancies)
-
- [RemoveRedundancies](inv:#*.passes.RemoveRedundancies)
:::

- \[1\] If no value is specified then `optimisation_level` defaults to a value of 2.
Expand Down
33 changes: 30 additions & 3 deletions pytket/extensions/quantinuum/backends/quantinuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
DecomposeTK2,
FlattenRelabelRegistersPass,
FullPeepholeOptimise,
GreedyPauliSimp,
NormaliseTK2,
RemoveBarriers,
RemoveRedundancies,
SequencePass,
SimplifyInitial,
Expand Down Expand Up @@ -655,14 +657,16 @@ def rebase_pass(self) -> BasePass:
allow_swaps=self.compilation_config.allow_implicit_swaps,
)

def default_compilation_pass(self, optimisation_level: int = 2) -> BasePass:
def default_compilation_pass(
self, optimisation_level: int = 2, timeout: int = 300
) -> BasePass:
"""
:param optimisation_level: Allows values of 0,1 or 2, with higher values
prompting more computationally heavy optimising compilation that
can lead to reduced gate count in circuits.
:return: Compilation pass for compiling circuits to Quantinuum devices
"""
assert optimisation_level in range(3)
assert optimisation_level in range(4)
passlist = [
DecomposeBoxes(),
scratch_reg_resize_pass(),
Expand Down Expand Up @@ -710,7 +714,7 @@ def default_compilation_pass(self, optimisation_level: int = 2) -> BasePass:
RemoveRedundancies(),
]
)
else:
elif optimisation_level == 2:
passlist.append(
FullPeepholeOptimise(
allow_swaps=self.compilation_config.allow_implicit_swaps,
Expand All @@ -726,6 +730,29 @@ def default_compilation_pass(self, optimisation_level: int = 2) -> BasePass:
RemoveRedundancies(),
]
)
else:
passlist.extend(
[
RemoveBarriers(),
AutoRebase({OpType.CX, OpType.Rz, OpType.H}),
GreedyPauliSimp(
allow_zzphase=True,
only_reduce=True,
thread_timeout=timeout,
trials=10,
),
]
)
passlist.extend(decomposition_passes)
passlist.extend(
[
self.rebase_pass(),
RemoveRedundancies(),
squash,
RemoveRedundancies(),
]
)

# In TKET, a qubit register with N qubits can have qubits
# indexed with a a value greater than N, i.e. a single
# qubit register can exist with index "7" or similar.
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1534,3 +1534,30 @@ def test_view_calendar(authenticated_quum_handler: QuantinuumAPI) -> Any:
api_handler=authenticated_quum_handler, device_name="H1-1"
)
return backend.view_calendar(month=2, year=2024)


@pytest.mark.parametrize(
"authenticated_quum_backend_qa", [{"device_name": "H1-1E"}], indirect=True
)
def test_optimisation_level_3_compilation() -> None:
b = authenticated_quum_backend_prod

c = Circuit(6)
c.add_barrier([0, 1, 2, 3, 4, 5])
for _ in range(6):
for i in range(4):
for j in range(i + 1, 4):
c.CX(i, j)
c.Rz(0.23, j)
c.S(j)
c.H(i)

compiled_2 = b.get_compiled_circuit(c, 2)
compiled_3 = b.get_compiled_circuit(c, 3)

assert compiled_3.n_2qb_gates() == 36
assert compiled_2.n_gates == 97
assert compiled_2.depth() == 45
assert compiled_3.n_2qb_gates() == 31
assert compiled_3.n_gates == 93
assert compiled_3.depth() == 49

0 comments on commit 3ef56a6

Please sign in to comment.