Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into remove-networkx-optio…
Browse files Browse the repository at this point in the history
…nal-usage
  • Loading branch information
mtreinish committed Nov 3, 2022
2 parents 2f06133 + 45f7836 commit 92e50e8
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions qiskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# find compiled submodules in _accelerate because it relies on file paths
# manually define them on import so people can directly import
# qiskit._accelerate.* submodules and not have to rely on attribute access
sys.modules["qiskit._accelerate.nlayout"] = qiskit._accelerate.nlayout
sys.modules["qiskit._accelerate.stochastic_swap"] = qiskit._accelerate.stochastic_swap
sys.modules["qiskit._accelerate.sabre_swap"] = qiskit._accelerate.sabre_swap
sys.modules["qiskit._accelerate.pauli_expval"] = qiskit._accelerate.pauli_expval
Expand Down
5 changes: 4 additions & 1 deletion qiskit/pulse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def visit_Expression(self, node: ast.Expression) -> ast.Expression:
Returns:
Evaluated value.
"""
tmp_node = copy.deepcopy(node)
tmp_node = copy.copy(node)
tmp_node.body = self.visit(tmp_node.body)

return tmp_node
Expand Down Expand Up @@ -240,6 +240,7 @@ def visit_UnaryOp(self, node: ast.UnaryOp) -> Union[ast.UnaryOp, ast.Constant]:
Returns:
Evaluated value.
"""
node = copy.copy(node)
node.operand = self.visit(node.operand)
if isinstance(node.operand, (ast.Constant, ast.Num)):
val = ast.Constant(n=self._match_ops(node.op, self._unary_ops, node.operand.n))
Expand All @@ -255,6 +256,7 @@ def visit_BinOp(self, node: ast.BinOp) -> Union[ast.BinOp, ast.Constant]:
Returns:
Evaluated value.
"""
node = copy.copy(node)
node.left = self.visit(node.left)
node.right = self.visit(node.right)
if isinstance(node.left, (ast.Constant, ast.Num)) and isinstance(
Expand All @@ -280,6 +282,7 @@ def visit_Call(self, node: ast.Call) -> Union[ast.Call, ast.Constant]:
"""
if not isinstance(node.func, ast.Name):
raise PulseError("Unsafe expression is detected.")
node = copy.copy(node)
node.args = [self.visit(arg) for arg in node.args]
if all(isinstance(arg, (ast.Constant, ast.Num)) for arg in node.args):
if node.func.id not in self._math_ops.keys():
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/passes/routing/sabre_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
NeighborTable,
SabreDAG,
)
from qiskit._accelerate.stochastic_swap import NLayout
from qiskit._accelerate.nlayout import NLayout

logger = logging.getLogger(__name__)

Expand Down
3 changes: 2 additions & 1 deletion qiskit/transpiler/passes/routing/stochastic_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from qiskit.transpiler.layout import Layout
from qiskit.circuit import IfElseOp, WhileLoopOp, ForLoopOp, ControlFlowOp, Instruction
from qiskit._accelerate import stochastic_swap as stochastic_swap_rs
from qiskit._accelerate import nlayout

from .utils import get_swap_map_dag

Expand Down Expand Up @@ -191,7 +192,7 @@ def _layer_permutation(self, layer_partition, layout, qubit_subset, coupling, tr
)

layout_mapping = {self._qubit_to_int[k]: v for k, v in layout.get_virtual_bits().items()}
int_layout = stochastic_swap_rs.NLayout(layout_mapping, num_qubits, coupling.size())
int_layout = nlayout.NLayout(layout_mapping, num_qubits, coupling.size())

trial_circuit = DAGCircuit() # SWAP circuit for slice of swaps in this trial
trial_circuit.add_qubits(layout.get_virtual_bits())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
The Pulse expression parser should no longer periodically hang when called
from Jupyter notebooks. This is achieved by avoiding an internal ``deepycopy``
of a recursive object that seemed to be particularly difficult for the
memoization to evaluate.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub fn getenv_use_multiple_threads() -> bool {

#[pymodule]
fn _accelerate(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pymodule!(nlayout::nlayout))?;
m.add_wrapped(wrap_pymodule!(stochastic_swap::stochastic_swap))?;
m.add_wrapped(wrap_pymodule!(sabre_swap::sabre_swap))?;
m.add_wrapped(wrap_pymodule!(pauli_exp_val::pauli_expval))?;
Expand Down
6 changes: 6 additions & 0 deletions src/nlayout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,9 @@ impl NLayout {
self.clone()
}
}

#[pymodule]
pub fn nlayout(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<NLayout>()?;
Ok(())
}
1 change: 0 additions & 1 deletion src/stochastic_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ pub fn swap_trials(
#[pymodule]
pub fn stochastic_swap(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(swap_trials))?;
m.add_class::<NLayout>()?;
m.add_class::<EdgeCollection>()?;
Ok(())
}

0 comments on commit 92e50e8

Please sign in to comment.