Skip to content

Commit

Permalink
Add option to set a minimum size to unitarysynthesis pass
Browse files Browse the repository at this point in the history
This commit adds a new option to the UnitarySynthesis pass constructor,
min_qubits, which is used to specify a minimimum size unitary to
synthesize. If the unitary is smaller than that it will be skipped. This
is then used by the UnitarySynthesis instance in the unroll3q phase so
we don't decompose 1 or 2q unitaries before routing.
  • Loading branch information
mtreinish committed May 19, 2021
1 parent c5a2b56 commit 401aedf
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
10 changes: 9 additions & 1 deletion qiskit/transpiler/passes/synthesis/unitary_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def _choose_euler_basis(basis_gates):
class UnitarySynthesis(TransformationPass):
"""Synthesize gates according to their basis gates."""

def __init__(self, basis_gates: List[str], approximation_degree: float = 1):
def __init__(
self, basis_gates: List[str], approximation_degree: float = 1, min_qubits: int = None
):
"""
Synthesize unitaries over some basis gates.
Expand All @@ -68,10 +70,14 @@ def __init__(self, basis_gates: List[str], approximation_degree: float = 1):
Args:
basis_gates: List of gate names to target.
approximation_degree: closeness of approximation (0: lowest, 1: highest).
min_qubits: The minimum number of qubits in the unitary to synthesize. If this is set
and the unitary is less than the specified number of qubits it will not be
synthesized.
"""
super().__init__()
self._basis_gates = basis_gates
self._approximation_degree = approximation_degree
self._min_qubits = min_qubits

def run(self, dag: DAGCircuit) -> DAGCircuit:
"""Run the UnitarySynthesis pass on `dag`.
Expand All @@ -92,6 +98,8 @@ def run(self, dag: DAGCircuit) -> DAGCircuit:
decomposer2q = TwoQubitBasisDecomposer(kak_gate, euler_basis=euler_basis)

for node in dag.named_nodes("unitary"):
if self._min_qubits is not None and len(node.qargs) < self._min_qubits:
continue

synth_dag = None
if len(node.qargs) == 1:
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/preset_passmanagers/level0.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _choose_layout_condition(property_set):
# 3. Decompose so only 1-qubit and 2-qubit gates remain
_unroll3q = [
# Use unitary synthesis for basis aware decomposition of UnitaryGates
UnitarySynthesis(basis_gates, approximation_degree=approximation_degree),
UnitarySynthesis(basis_gates, approximation_degree=approximation_degree, min_qubits=3),
Unroll3qOrMore(),
]

Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/preset_passmanagers/level1.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def _not_perfect_yet(property_set):
# 4. Decompose so only 1-qubit and 2-qubit gates remain
_unroll3q = [
# Use unitary synthesis for basis aware decomposition of UnitaryGates
UnitarySynthesis(basis_gates, approximation_degree=approximation_degree),
UnitarySynthesis(basis_gates, approximation_degree=approximation_degree, min_qubits=3),
Unroll3qOrMore(),
]

Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/preset_passmanagers/level2.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def _csp_not_found_match(property_set):
# 3. Unroll to 1q or 2q gates
_unroll3q = [
# Use unitary synthesis for basis aware decomposition of UnitaryGates
UnitarySynthesis(basis_gates, approximation_degree=approximation_degree),
UnitarySynthesis(basis_gates, approximation_degree=approximation_degree, min_qubits=3),
Unroll3qOrMore(),
]

Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/preset_passmanagers/level3.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def level_3_pass_manager(pass_manager_config: PassManagerConfig) -> PassManager:
# 1. Unroll to 1q or 2q gates
_unroll3q = [
# Use unitary synthesis for basis aware decomposition of UnitaryGates
UnitarySynthesis(basis_gates, approximation_degree=approximation_degree),
UnitarySynthesis(basis_gates, approximation_degree=approximation_degree, min_qubits=3),
Unroll3qOrMore(),
]

Expand Down
9 changes: 9 additions & 0 deletions releasenotes/notes/squ-gate-name-785b7896300a92ef.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
---
features:
- |
The :class:`~qiskit.transpiler.passes.UnitarySynthesis` transpiler pass in
:mod:`qiskit.transpiler.passes` has a new kwarg in the constructor,
``min_qubits``. When specified this can be set to an ``int`` value which
is the minimum size :class:`~qiskit.extensions.UnitaryGate` object to
run the unitary synthesis on. If a :class:`~qiskit.extensions.UnitaryGate`
in a :class:`~qiskit.circuit.QuantumCircuit` uses fewer qubits it will
be skipped by that instance of the pass.
upgrade:
- |
The :attr:`~qiskit.extensions.SingleQubitUnitary.name` attribute of the
Expand Down

0 comments on commit 401aedf

Please sign in to comment.