Skip to content

Commit

Permalink
Merge pull request #162 from unitaryfund/70-reorganize-code-structure
Browse files Browse the repository at this point in the history
Reorganize code structure
  • Loading branch information
Misty-W authored Jan 8, 2025
2 parents 0575072 + 384d2bf commit 34bae29
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 190 deletions.
8 changes: 2 additions & 6 deletions docs/source/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ UCC includes the following modules:
- ``UnitarySynthesis``
- ``Optimize1qGatesDecomposition``
- ``CollectCliffords``
- ``circuit_passes`` containing functions for checking commutation relations
- ``transpiler_passes`` consisting of submodules, each designed to perform a different optimization pass on the circuit.
- ``HighLevelSynthesis`` (greedy Clifford synthesis)
- ``transpiler_passes`` consisting of submodules, each designed to perform a different optimization or analysis pass on the circuit.

These include the passes listed in ``UCC_Default1``, along with others for specialized use.
The full list of transpiler passes available in UCC can be found in the :doc:`api`.
Expand All @@ -75,16 +75,12 @@ UCC settings can be adjusted using the keyword arguments of the ``ucc.compile()`
ucc.compile(
circuit,
return_format="original",
mode="ucc",
target_device=None,
get_gate_counts=False,
)
- ``return_format`` is the format in which the input circuit will be returned, e.g. "TKET" or "OpenQASM2". Check ``ucc.supported_circuit_formats()`` for supported circuit formats. Default is the format of input circuit.
- ``mode`` specifies transpiler mode to use; currently ``ucc`` and ``qiskit`` are supported.
- ``target_device`` can be specified as a Qiskit backend or coupling map, or a list of connections between qubits. If None, all-to-all connectivity is assumed. If a Qiskit backend or coupling map is specified, only the coupling list extracted from the backend is used.
- ``get_gate_counts`` - if ``True``, gate counts of the compiled circuit are returned along with the compiled circuit. Default is ``False``.

Writing a custom pass
=====================
Expand Down
1 change: 0 additions & 1 deletion ucc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .transpilers import UCCTranspiler
from .compile import compile, supported_circuit_formats

from ucc._version import __version__
1 change: 0 additions & 1 deletion ucc/circuit_passes/__init__.py

This file was deleted.

55 changes: 39 additions & 16 deletions ucc/compile.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
from ucc.transpilers import UCCTranspiler
from qbraid.transpiler import transpile
from qiskit.providers import BackendV2
from qiskit.transpiler import CouplingMap
from qbraid.programs.alias_manager import get_program_type_alias
from qbraid.transpiler import ConversionGraph
from qbraid.transpiler import transpile
from .transpilers.ucc_defaults import UCCDefault1

supported_circuit_formats = ConversionGraph().nodes()


def compile(
circuit,
return_format="original",
mode="ucc",
target_device=None,
get_gate_counts=False,
):
"""Compiles the provided quantum `circuit` by translating it to a Qiskit circuit,
transpiling using the specified `mode`, and returning the optimized circuit in
the specified `return_format`.
"""Compiles the provided quantum `circuit` by translating it to a Qiskit
circuit, transpiling it, and returning the optimized circuit in the
specified `return_format`.
Args:
circuit (object): The quantum circuit to be compiled.
return_format (str): The format in which your circuit will be returned.
e.g., "TKET", "OpenQASM2". Check ``ucc.supported_circuit_formats()``.
Defaults to the format of the input circuit.
mode (str): Specifies the transpiler mode to use, either 'ucc' or 'qiskit'.
Returns:
object: The compiled circuit in the specified format.
Expand All @@ -32,16 +31,40 @@ def compile(

# Translate to Qiskit Circuit object
qiskit_circuit = transpile(circuit, "qiskit")
compiled_circuit, gate_counts = UCCTranspiler.transpile(
compiled_circuit = UCCDefault1().run(
qiskit_circuit,
mode=mode,
get_gate_counts=get_gate_counts,
target_device=target_device,
coupling_list=get_backend_connectivity(target_device),
)

# Translate the compiled circuit to the desired format
final_result = transpile(compiled_circuit, return_format)
if get_gate_counts:
return final_result, gate_counts
else:
return final_result
return final_result


def get_backend_connectivity(target_device = None) -> str:
"""
Extracts the coupling graph from the provided device in the form of a list of connections between qubits.
Parameters:
target_device: Can be a Qiskit backend or Qiskit CouplingMap, or a list of connections between qubits.
If None, all-to-all connectivity is assumed.
If Qiskit backend or coupling map, only the coupling list extracted from the backend is used.
Returns:
coupling_list: The list of connections between qubits.
"""

if target_device is None:
coupling_list = None
elif isinstance(target_device, BackendV2):
coupling_list = list(target_device.coupling_map.get_edges())
elif isinstance(target_device, CouplingMap):
# rustworkx.EdgeList object
coupling_list = target_device.get_edges()
elif isinstance(target_device, list):
# user-specified list of edges of coupling graph
coupling_list = target_device
else:
raise ValueError("Invalid backend type. Must be a Qiskit backend, coupling map, or a list of connections between qubits.")

return coupling_list
3 changes: 1 addition & 2 deletions ucc/transpiler_passes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from .basis_translator import BasisTranslator
from .commutative_cancellation import CommutativeCancellation
from .commutation_checker import CommutationChecker
from .collect_2q_blocks import Collect2qBlocks
from .commutation_analysis import CommutationAnalysis
# from .consolidate_blocks import ConsolidateBlocks
from .unitary_synthesis import UnitarySynthesis
from .collect_2q_blocks import Collect2qBlocks
from .collect_1q_runs import Collect1qRuns
from .optimize_1q_decomposition import Optimize1qGatesDecomposition
from .custom_cx import CXCancellation
from .spectral_mapping import SpectralMapping
from .sabre_layout import SabreLayout
2 changes: 1 addition & 1 deletion ucc/transpiler_passes/commutation_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from qiskit.dagcircuit import DAGOpNode
from qiskit.transpiler.basepasses import AnalysisPass

from ..circuit_passes import CommutationChecker
from .commutation_checker import CommutationChecker

from qiskit.circuit._standard_gates_commutations import standard_gates_commutations

Expand Down
File renamed without changes.
105 changes: 0 additions & 105 deletions ucc/transpiler_passes/custom_cx.py

This file was deleted.

1 change: 0 additions & 1 deletion ucc/transpilers/__init__.py

This file was deleted.

2 changes: 1 addition & 1 deletion ucc/transpilers/ucc_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# from ucc.transpiler_passes.sabre_swap import SabreSwap


from ..transpiler_passes import CommutativeCancellation, Collect2qBlocks, UnitarySynthesis, Optimize1qGatesDecomposition, CXCancellation, SpectralMapping
from ..transpiler_passes import CommutativeCancellation, Collect2qBlocks, UnitarySynthesis, Optimize1qGatesDecomposition, SpectralMapping

from qiskit.transpiler.passes import Optimize1qGatesSimpleCommutation, ElidePermutations

Expand Down
56 changes: 0 additions & 56 deletions ucc/transpilers/ucc_transpiler.py

This file was deleted.

0 comments on commit 34bae29

Please sign in to comment.