Skip to content

Commit b83327a

Browse files
authored
Fix TKet error related to imports (#470)
* Fix TKet error related to imports - TKet objects, such as Architecture, were being unresolved due to the fallback structure of the try/except blocks for ImportError. - This PR cleans up the fallback structure and makes the dependency resolution for pytket a bit more robust. - This should allow this file to be parsed by our documentation system. * Fix for new diagrams.
1 parent 1a8eda4 commit b83327a

2 files changed

Lines changed: 54 additions & 24 deletions

File tree

recirq/qaoa/placement.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,39 @@
1818
from cirq import RouteCQC
1919
except ImportError:
2020
RouteCQC = NotImplemented
21+
22+
try:
23+
# Set the 'RECIRQ_IMPORT_FAILSAFE' environment variable to treat PyTket as an optional
24+
# dependency. We do this for CI testing against the next, pre-release Cirq version.
25+
import pytket
26+
import pytket.extensions.cirq
27+
from pytket.circuit import Node, Qubit
28+
from pytket.passes import SequencePass, RoutingPass, PlacementPass
29+
from pytket.predicates import CompilationUnit, ConnectivityPredicate
2130
try:
22-
# Set the 'RECIRQ_IMPORT_FAILSAFE' environment variable to treat PyTket as an optional
23-
# dependency. We do this for CI testing against the next, pre-release Cirq version.
24-
import pytket
25-
import pytket.extensions.cirq
26-
from pytket.circuit import Node, Qubit
27-
from pytket.passes import SequencePass, RoutingPass, PlacementPass
28-
from pytket.predicates import CompilationUnit, ConnectivityPredicate
29-
try:
30-
from pytket.placement import GraphPlacement
31-
except ImportError:
32-
from pytket.routing import GraphPlacement
33-
34-
try:
35-
from pytket.architecture import Architecture
36-
except ImportError:
37-
from pytket.routing import Architecture
31+
from pytket.placement import GraphPlacement
3832
except ImportError:
39-
if 'RECIRQ_IMPORT_FAILSAFE' in os.environ:
40-
pytket = NotImplemented
41-
else:
42-
raise ImportError(
43-
"Routing utilities don't exist in this version of Cirq and PyTket is not installed."
44-
)
33+
from pytket.routing import GraphPlacement
34+
35+
try:
36+
from pytket.architecture import Architecture
37+
except ImportError:
38+
from pytket.routing import Architecture
39+
except ImportError:
40+
pytket = NotImplemented
41+
Node = NotImplemented
42+
Qubit = NotImplemented
43+
SequencePass = NotImplemented
44+
RoutingPass = NotImplemented
45+
PlacementPass = NotImplemented
46+
CompilationUnit = NotImplemented
47+
ConnectivityPredicate = NotImplemented
48+
GraphPlacement = NotImplemented
49+
Architecture = NotImplemented
50+
if 'RECIRQ_IMPORT_FAILSAFE' not in os.environ and RouteCQC is NotImplemented:
51+
raise ImportError(
52+
"Routing utilities don't exist in this version of Cirq and PyTket is not installed."
53+
)
4554

4655
import recirq
4756

@@ -69,6 +78,10 @@ def calibration_data_to_graph(calib_dict: cg.Calibration) -> nx.Graph:
6978

7079
def _qubit_index_edges(device: cirq.Device):
7180
"""Helper function in `_device_to_tket_device`."""
81+
if Node is NotImplemented:
82+
raise ImportError(
83+
"pytket-cirq is required for this function but not installed."
84+
)
7285
qubits = device.metadata.qubit_set if device.metadata else ()
7386
dev_graph = ccr.gridqubits_to_graph_device(qubits)
7487
for n1, n2 in dev_graph.edges:
@@ -80,6 +93,10 @@ def _device_to_tket_device(device: cirq.Device):
8093
8194
This supports any device that supports `ccr.xmon_device_to_graph`.
8295
"""
96+
if Architecture is NotImplemented:
97+
raise ImportError(
98+
"pytket-cirq is required for this function but not installed."
99+
)
83100
return Architecture(
84101
list(_qubit_index_edges(device))
85102
)

recirq/qaoa/problem_circuits_test.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_get_routed_grid_model_circuit():
4040
betas=[np.pi / 2, np.pi / 4],
4141
)
4242

43-
cirq.testing.assert_has_diagram(circuit, """
43+
expected_diagram = """
4444
(0, 0) (0, 1) (0, 2) (1, 0) (1, 1) (1, 2)
4545
│ │ │ │ │ │
4646
H H H H H H
@@ -73,7 +73,20 @@ def test_get_routed_grid_model_circuit():
7373
│ │ │ │ │ │
7474
X^0.5 X^0.5 X^0.5 X^0.5 X^0.5 X^0.5
7575
│ │ │ │ │ │
76-
""", transpose=True)
76+
"""
77+
78+
try:
79+
cirq.testing.assert_has_diagram(circuit, expected_diagram, transpose=True)
80+
except AssertionError:
81+
# Fallback for newer Cirq versions that render ZZ**-1 as ZZ^-1
82+
expected_diagram = expected_diagram.replace(
83+
'ZZ─────ZZ │ ZZ─────ZZ',
84+
'ZZ─────ZZ^-1 │ ZZ─────ZZ'
85+
).replace(
86+
'ZZ │ │ │',
87+
'ZZ^-1 │ │ │'
88+
)
89+
cirq.testing.assert_has_diagram(circuit, expected_diagram, transpose=True)
7790

7891

7992
def test_get_compiled_grid_model_circuit():

0 commit comments

Comments
 (0)