Skip to content

Commit

Permalink
Switch from retworkx to rustworkx package
Browse files Browse the repository at this point in the history
In the retworkx/rustworkx 0.12.0 release the package was renamed from
retworkx to rustworkx which was done at the request of the networkx
maintainers [1]. While the retworkx name continues to work for backwards
compatibility, it is best to move off of the legacy name and start using
the new name sooner rather than later. This commit updates all the
retworkx usage to the new rustworkx and updates our requirements list to
directly depend on rustworkx.

[1] https://qiskit.org/documentation/rustworkx/release_notes.html#prelude
  • Loading branch information
mtreinish committed Nov 17, 2022
1 parent 2b7e998 commit 4111f21
Show file tree
Hide file tree
Showing 42 changed files with 104 additions and 79 deletions.
21 changes: 17 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ rand_distr = "0.4.3"
ahash = "0.8.0"
num-complex = "0.4"
num-bigint = "0.4"
retworkx-core = "0.11"
rustworkx-core = "0.12"

[dependencies.pyo3]
version = "0.17.3"
Expand Down
4 changes: 2 additions & 2 deletions qiskit/circuit/equivalence.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

from collections import namedtuple

from retworkx.visualization import graphviz_draw # pylint: disable=no-name-in-module
import retworkx as rx
from rustworkx.visualization import graphviz_draw # pylint: disable=no-name-in-module
import rustworkx as rx

from qiskit.exceptions import InvalidFileError
from .exceptions import CircuitError
Expand Down
2 changes: 1 addition & 1 deletion qiskit/circuit/library/graph_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class GraphState(QuantumCircuit):
from qiskit.circuit.library import GraphState
import qiskit.tools.jupyter
import retworkx as rx
import rustworkx as rx
G = rx.generators.cycle_graph(5)
circuit = GraphState(rx.adjacency_matrix(G))
%circuit_library_info circuit
Expand Down
12 changes: 6 additions & 6 deletions qiskit/dagcircuit/dagcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import math

import numpy as np
import retworkx as rx
import rustworkx as rx

from qiskit.circuit import ControlFlowOp, ForLoopOp, IfElseOp, WhileLoopOp
from qiskit.circuit.exceptions import CircuitError
Expand Down Expand Up @@ -1240,7 +1240,7 @@ def substitute_node_with_dag(self, node, input_dag, wires=None, propagate_condit
self.global_phase += in_dag.global_phase

# Add wire from pred to succ if no ops on mapped wire on ``in_dag``
# retworkx's substitute_node_with_subgraph lacks the DAGCircuit
# rustworkx's substitute_node_with_subgraph lacks the DAGCircuit
# context to know what to do in this case (the method won't even see
# these nodes because they're filtered) so we manually retain the
# edges prior to calling substitute_node_with_subgraph and set the
Expand Down Expand Up @@ -1276,17 +1276,17 @@ def edge_map_fn(source, _target, self_wire):
wire_output_id = in_dag.output_map[wire]._node_id
out_index = in_dag._multi_graph.predecessor_indices(wire_output_id)[0]
# Edge directly from from input nodes to output nodes in in_dag are
# already handled prior to calling retworkx. Don't map these edges
# in retworkx.
# already handled prior to calling rustworkx. Don't map these edges
# in rustworkx.
if not isinstance(in_dag._multi_graph[out_index], DAGOpNode):
return None
# predecessor edge
else:
wire_input_id = in_dag.input_map[wire]._node_id
out_index = in_dag._multi_graph.successor_indices(wire_input_id)[0]
# Edge directly from from input nodes to output nodes in in_dag are
# already handled prior to calling retworkx. Don't map these edges
# in retworkx.
# already handled prior to calling rustworkx. Don't map these edges
# in rustworkx.
if not isinstance(in_dag._multi_graph[out_index], DAGOpNode):
return None
return out_index
Expand Down
2 changes: 1 addition & 1 deletion qiskit/dagcircuit/dagdependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import heapq
from collections import OrderedDict, defaultdict

import retworkx as rx
import rustworkx as rx

from qiskit.circuit.quantumregister import QuantumRegister, Qubit
from qiskit.circuit.classicalregister import ClassicalRegister, Clbit
Expand Down
2 changes: 1 addition & 1 deletion qiskit/opflow/converters/abelian_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import List, Tuple, Union, cast

import numpy as np
import retworkx as rx
import rustworkx as rx

from qiskit.opflow.converters.converter_base import ConverterBase
from qiskit.opflow.evolutions.evolved_op import EvolvedOp
Expand Down
2 changes: 1 addition & 1 deletion qiskit/pulse/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from typing import List, Tuple, Iterable, Union, Dict, Callable, Set, Optional, Any

import numpy as np
import retworkx as rx
import rustworkx as rx

from qiskit.circuit.parameter import Parameter
from qiskit.circuit.parameterexpression import ParameterExpression, ParameterValueType
Expand Down
2 changes: 1 addition & 1 deletion qiskit/pulse/transforms/dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# that they have been altered from the originals.
"""A collection of functions to convert ScheduleBlock to DAG representation."""

import retworkx as rx
import rustworkx as rx

from qiskit.pulse.exceptions import UnassignedReferenceError

Expand Down
4 changes: 2 additions & 2 deletions qiskit/quantum_info/operators/symplectic/pauli_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from collections import defaultdict

import numpy as np
import retworkx as rx
import rustworkx as rx

from qiskit.exceptions import QiskitError
from qiskit.quantum_info.operators.custom_iterator import CustomIterator
Expand Down Expand Up @@ -1109,7 +1109,7 @@ def _create_graph(self, qubit_wise):
or on a per-qubit basis.
Returns:
retworkx.PyGraph: A class of undirected graphs
rustworkx.PyGraph: A class of undirected graphs
"""

edges = self._noncommutation_graph(qubit_wise)
Expand Down
4 changes: 2 additions & 2 deletions qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from typing import Dict, Optional

import numpy as np
import retworkx as rx
import rustworkx as rx

from qiskit._accelerate.sparse_pauli_op import unordered_unique
from qiskit.exceptions import QiskitError
Expand Down Expand Up @@ -930,7 +930,7 @@ def _create_graph(self, qubit_wise):
or on a per-qubit basis.
Returns:
retworkx.PyGraph: A class of undirected graphs
rustworkx.PyGraph: A class of undirected graphs
"""

edges = self.paulis._noncommutation_graph(qubit_wise)
Expand Down
8 changes: 4 additions & 4 deletions qiskit/transpiler/coupling.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import warnings

import numpy as np
import retworkx as rx
from retworkx.visualization import graphviz_draw # pylint: disable=no-name-in-module
import rustworkx as rx
from rustworkx.visualization import graphviz_draw # pylint: disable=no-name-in-module

from qiskit.transpiler.exceptions import CouplingError

Expand Down Expand Up @@ -412,8 +412,8 @@ def __str__(self):
def draw(self):
"""Draws the coupling map.
This function calls the :func:`~retworkx.visualization.graphviz_draw` function from the
``retworkx`` package to draw the :class:`CouplingMap` object.
This function calls the :func:`~rustworkx.visualization.graphviz_draw` function from the
``rustworkx`` package to draw the :class:`CouplingMap` object.
Returns:
PIL.Image: Drawn coupling map.
Expand Down
14 changes: 7 additions & 7 deletions qiskit/transpiler/passes/basis/basis_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from itertools import zip_longest
from collections import defaultdict

import retworkx
import rustworkx

from qiskit.circuit import Gate, ParameterVector, QuantumRegister, ControlFlowOp, QuantumCircuit
from qiskit.dagcircuit import DAGCircuit
Expand Down Expand Up @@ -364,11 +364,11 @@ def _extract_basis_target(


class StopIfBasisRewritable(Exception):
"""Custom exception that signals `retworkx.dijkstra_search` to stop."""
"""Custom exception that signals `rustworkx.dijkstra_search` to stop."""


class BasisSearchVisitor(retworkx.visit.DijkstraVisitor): # pylint: disable=no-member
"""Handles events emitted during `retworkx.dijkstra_search`."""
class BasisSearchVisitor(rustworkx.visit.DijkstraVisitor): # pylint: disable=no-member
"""Handles events emitted during `rustworkx.dijkstra_search`."""

def __init__(self, graph, source_basis, target_basis, num_gates_for_rule):
self.graph = graph
Expand Down Expand Up @@ -413,7 +413,7 @@ def examine_edge(self, edge):
# if there are gates in this `rule` that we have not yet generated, we can't apply
# this `rule`. if `target` is already in basis, it's not beneficial to use this rule.
if self._num_gates_remain_for_rule[index] > 0 or target in self.target_basis:
raise retworkx.visit.PruneSearch # pylint: disable=no-member
raise rustworkx.visit.PruneSearch # pylint: disable=no-member

def edge_relaxed(self, edge):
_, target, edata = edge
Expand Down Expand Up @@ -479,7 +479,7 @@ def _basis_search(equiv_lib, source_basis, target_basis):

all_gates_in_lib = set()

graph = retworkx.PyDiGraph()
graph = rustworkx.PyDiGraph()
nodes_to_indices = dict()
num_gates_for_rule = dict()

Expand Down Expand Up @@ -526,7 +526,7 @@ def lazy_setdefault(key):
graph.add_edges_from_no_data([(dummy, nodes_to_indices[key]) for key in target_basis_keys])
rtn = None
try:
retworkx.digraph_dijkstra_search(graph, [dummy], vis.edge_cost, vis)
rustworkx.digraph_dijkstra_search(graph, [dummy], vis.edge_cost, vis)
except StopIfBasisRewritable:
rtn = vis.basis_transforms

Expand Down
6 changes: 3 additions & 3 deletions qiskit/transpiler/passes/layout/dense_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


import numpy as np
import retworkx
import rustworkx

from qiskit.transpiler.layout import Layout
from qiskit.transpiler.basepasses import AnalysisPass
Expand Down Expand Up @@ -53,12 +53,12 @@ def __init__(self, coupling_map=None, backend_prop=None, target=None):
num_qubits = target.num_qubits
self.coupling_map = target.build_coupling_map()
if self.coupling_map is not None:
self.adjacency_matrix = retworkx.adjacency_matrix(self.coupling_map.graph)
self.adjacency_matrix = rustworkx.adjacency_matrix(self.coupling_map.graph)
self.error_mat, self._use_error = _build_error_matrix(num_qubits, target=target)
else:
if self.coupling_map:
num_qubits = self.coupling_map.size()
self.adjacency_matrix = retworkx.adjacency_matrix(self.coupling_map.graph)
self.adjacency_matrix = rustworkx.adjacency_matrix(self.coupling_map.graph)
self.error_mat, self._use_error = _build_error_matrix(
num_qubits, backend_prop=self.backend_prop, coupling_map=self.coupling_map
)
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/passes/layout/noise_adaptive_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import math

import retworkx as rx
import rustworkx as rx

from qiskit.transpiler.layout import Layout
from qiskit.transpiler.basepasses import AnalysisPass
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/passes/layout/vf2_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import logging
import time

from retworkx import vf2_mapping
from rustworkx import vf2_mapping

from qiskit.transpiler.layout import Layout
from qiskit.transpiler.basepasses import AnalysisPass
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/passes/layout/vf2_post_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import inspect
import time

from retworkx import PyDiGraph, vf2_mapping, PyGraph
from rustworkx import PyDiGraph, vf2_mapping, PyGraph

from qiskit.transpiler.layout import Layout
from qiskit.transpiler.basepasses import AnalysisPass
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/passes/layout/vf2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import statistics
import random

from retworkx import PyDiGraph, PyGraph
from rustworkx import PyDiGraph, PyGraph

from qiskit.circuit import ControlFlowOp, ForLoopOp
from qiskit.converters import circuit_to_dag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from typing import Iterator, Mapping, MutableMapping, MutableSet, List, Iterable, Union

import numpy as np
import retworkx as rx
import rustworkx as rx

from .types import Swap, Permutation
from .util import PermutationCircuit, permutation_circuit
Expand Down
6 changes: 4 additions & 2 deletions qiskit/transpiler/passes/routing/sabre_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import logging
from copy import copy, deepcopy

import retworkx
import rustworkx

from qiskit.circuit.library.standard_gates import SwapGate
from qiskit.transpiler.basepasses import TransformationPass
Expand Down Expand Up @@ -146,7 +146,9 @@ def __init__(self, coupling_map, heuristic="basic", seed=None, fake_run=False, t
self.coupling_map.make_symmetric()
self._neighbor_table = None
if coupling_map is not None:
self._neighbor_table = NeighborTable(retworkx.adjacency_matrix(self.coupling_map.graph))
self._neighbor_table = NeighborTable(
rustworkx.adjacency_matrix(self.coupling_map.graph)
)

self.heuristic = heuristic
self.seed = seed
Expand Down
6 changes: 3 additions & 3 deletions qiskit/transpiler/preset_passmanagers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,9 @@ def get_vf2_call_limit(
vf2_call_limit = None
if layout_method is None and initial_layout is None:
if optimization_level == 1:
vf2_call_limit = int(5e4) # Set call limit to ~100ms with retworkx 0.10.2
vf2_call_limit = int(5e4) # Set call limit to ~100ms with rustworkx 0.10.2
elif optimization_level == 2:
vf2_call_limit = int(5e6) # Set call limit to ~10 sec with retworkx 0.10.2
vf2_call_limit = int(5e6) # Set call limit to ~10 sec with rustworkx 0.10.2
elif optimization_level == 3:
vf2_call_limit = int(3e7) # Set call limit to ~60 sec with retworkx 0.10.2
vf2_call_limit = int(3e7) # Set call limit to ~60 sec with rustworkx 0.10.2
return vf2_call_limit
2 changes: 1 addition & 1 deletion qiskit/transpiler/preset_passmanagers/level1.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _vf2_match_not_found(property_set):
else VF2Layout(
coupling_map,
seed=seed_transpiler,
call_limit=int(5e4), # Set call limit to ~100ms with retworkx 0.10.2
call_limit=int(5e4), # Set call limit to ~100ms with rustworkx 0.10.2
properties=backend_properties,
target=target,
)
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 @@ -114,7 +114,7 @@ def _vf2_match_not_found(property_set):
else VF2Layout(
coupling_map,
seed=seed_transpiler,
call_limit=int(5e6), # Set call limit to ~10 sec with retworkx 0.10.2
call_limit=int(5e6), # Set call limit to ~10 sec with rustworkx 0.10.2
properties=backend_properties,
target=target,
)
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 @@ -120,7 +120,7 @@ def _vf2_match_not_found(property_set):
else VF2Layout(
coupling_map,
seed=seed_transpiler,
call_limit=int(3e7), # Set call limit to ~60 sec with retworkx 0.10.2
call_limit=int(3e7), # Set call limit to ~60 sec with rustworkx 0.10.2
properties=backend_properties,
target=target,
)
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import logging
import inspect

import retworkx as rx
import rustworkx as rx

from qiskit.circuit.parameter import Parameter
from qiskit.pulse.instruction_schedule_map import InstructionScheduleMap
Expand Down
Loading

0 comments on commit 4111f21

Please sign in to comment.