Skip to content

Commit

Permalink
Merge branch 'oxidize-commutative-cancellation' of https://github.com…
Browse files Browse the repository at this point in the history
…/sbrandhsn/qiskit into oxidize-commutative-cancellation
  • Loading branch information
sbrandhsn committed Sep 6, 2024
2 parents 8041ef7 + 10e7851 commit 858ca8f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
48 changes: 48 additions & 0 deletions crates/circuit/src/dag_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6393,4 +6393,52 @@ fn add_global_phase(py: Python, phase: &Param, other: &Param) -> PyResult<Param>
})
}

/// Add to global phase. Global phase can only be Float or ParameterExpression so this
/// does not handle the full possibility of parameter values.
fn add_global_phase(py: Python, phase: &Param, other: &Param) -> PyResult<Param> {
Ok(match [phase, other] {
[Param::Float(a), Param::Float(b)] => Param::Float(a + b),
[Param::Float(a), Param::ParameterExpression(b)] => Param::ParameterExpression(
b.clone_ref(py)
.call_method1(py, intern!(py, "__radd__"), (*a,))?,
),
[Param::ParameterExpression(a), Param::Float(b)] => Param::ParameterExpression(
a.clone_ref(py)
.call_method1(py, intern!(py, "__add__"), (*b,))?,
),
[Param::ParameterExpression(a), Param::ParameterExpression(b)] => {
Param::ParameterExpression(a.clone_ref(py).call_method1(
py,
intern!(py, "__add__"),
(b,),
)?)
}
_ => panic!("Invalid global phase"),
})
}

/// Add to global phase. Global phase can only be Float or ParameterExpression so this
/// does not handle the full possibility of parameter values.
fn add_global_phase(py: Python, phase: &Param, other: &Param) -> PyResult<Param> {
Ok(match [phase, other] {
[Param::Float(a), Param::Float(b)] => Param::Float(a + b),
[Param::Float(a), Param::ParameterExpression(b)] => Param::ParameterExpression(
b.clone_ref(py)
.call_method1(py, intern!(py, "__radd__"), (*a,))?,
),
[Param::ParameterExpression(a), Param::Float(b)] => Param::ParameterExpression(
a.clone_ref(py)
.call_method1(py, intern!(py, "__add__"), (*b,))?,
),
[Param::ParameterExpression(a), Param::ParameterExpression(b)] => {
Param::ParameterExpression(a.clone_ref(py).call_method1(
py,
intern!(py, "__add__"),
(b,),
)?)
}
_ => panic!("Invalid global phase"),
})
}

type SortKeyType<'a> = (&'a [Qubit], &'a [Clbit]);
17 changes: 17 additions & 0 deletions qiskit/visualization/circuit/circuit_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ def _generate_latex_source(
# matplotlib_circuit_drawer
# -----------------------------------------------------------------------------

_GLOBAL_NID = 0

def _matplotlib_circuit_drawer(
circuit,
Expand Down Expand Up @@ -707,6 +708,22 @@ def _matplotlib_circuit_drawer(
if fold is None:
fold = 25

# At its inception, the matplotlib drawer was written under the assumption
# that DAGNode equality would be referential, i.e. it uses dictionaries
# keyed by node instance for internal tracking. However, DAGNode now
# overrides __eq__ such that different instances can compare as equivalent.
# If all nodes were from the same DAG, we wouldn't have any problem here
# since nodes would be distinct by node ID. However, since each input layer
# comes from its own DAG, we can run into cases where two nodes in different
# layers compare as equivalent, and thus clobber each other in the drawer.
# To work around this, we assign new unique IDs to the nodes. Since DAGNode
# instances are ephemeral, this does not mutate the DAG.
global _GLOBAL_NID
for layer in nodes:
for node in layer:
node._node_id = _GLOBAL_NID
_GLOBAL_NID += 1

qcd = _matplotlib.MatplotlibDrawer(
qubits,
clbits,
Expand Down

0 comments on commit 858ca8f

Please sign in to comment.