Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions qiskit/converters/circuit_to_dagdependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ def circuit_to_dagdependency(circuit, create_preds_and_succs=True):
dagdependency._add_predecessors()
dagdependency._add_successors()

# copy global phase
dagdependency.global_phase = circuit.global_phase

return dagdependency
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ def run_dag_opt(self):
dag_dep_opt = DAGDependency()

dag_dep_opt.name = self.circuit_dag_dep.name
dag_dep_opt.global_phase = self.circuit_dag_dep.global_phase

qregs = list(self.circuit_dag_dep.qregs.values())
cregs = list(self.circuit_dag_dep.cregs.values())
Expand Down Expand Up @@ -428,6 +429,9 @@ def run_dag_opt(self):
inst = node.op.copy()
dag_dep_opt.add_op_node(inst.inverse(), qargs, cargs)

# Update global phase to account for the template's phase.
dag_dep_opt.global_phase -= group.template_dag_dep.global_phase

# Add the unmatched gates.
for node_id in self.unmatched_list:
node = self.circuit_dag_dep.get_node(node_id)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixed :func:`.circuit_to_dagdependency` failing to copy ``global_phase`` when converting
a template :class:`.QuantumCircuit` to a :class:`.DAGDependency`.

Fixes `#14537 <https://github.com/Qiskit/qiskit/issues/14537>`__.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
Fixed global phase handling in :class:`.TemplateSubstitution`, which previously
silently reset to input circuit's global phase to zero and didn't correct for global
phases of the templates.

Fixes `#14537 <https://github.com/Qiskit/qiskit/issues/14537>`__.
86 changes: 84 additions & 2 deletions test/python/transpiler/test_template_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ def test_consecutive_templates_do_not_apply(self):
qc.swap(0, 1)
qc.h(0)
qc_opt = pm.run(qc)
self.assertTrue(Operator(qc) == Operator(qc_opt))
self.assertEqual(Operator(qc), Operator(qc_opt))
Comment thread
Cryoris marked this conversation as resolved.

def test_clifford_templates(self):
"""Tests TemplateOptimization pass on several larger examples."""
Expand All @@ -758,10 +758,92 @@ def test_clifford_templates(self):
seed=seed,
)
qc_opt = pm.run(qc)
self.assertTrue(Operator(qc) == Operator(qc_opt))
self.assertEqual(Operator(qc), Operator(qc_opt))
# All of these gates are in the commutation library, i.e. the cache should not be used
self.assertEqual(scc.num_cached_entries(), 0)

def test_circuit_global_phase_preserved_after_single_and_multiple_template_match(self):
"""Test that circuit global_phase survives template optimization (#14537)."""

circuit_in = QuantumCircuit(2, global_phase=np.pi / 4)
circuit_in.cx(0, 1)
circuit_in.cx(0, 1)

circuit_in_mult = QuantumCircuit(2, global_phase=np.pi / 3)
# Two independent pairs of CX gates — the template will match twice.
circuit_in_mult.cx(0, 1)
circuit_in_mult.cx(0, 1)
circuit_in_mult.cx(0, 1)
circuit_in_mult.cx(0, 1)

template = QuantumCircuit(2)
template.cx(0, 1)
template.cx(0, 1)

result = TemplateOptimization([template])(circuit_in)

result_mult = TemplateOptimization([template])(circuit_in_mult)

with self.subTest(msg="single template match"):
self.assertAlmostEqual(float(result.global_phase), np.pi / 4)
self.assertEqual(result.count_ops(), {})

with self.subTest(msg="multiple template matches"):
self.assertAlmostEqual(float(result_mult.global_phase), np.pi / 3)
self.assertEqual(result_mult.count_ops(), {})

def test_template_nonzero_global_phase_applied_to_circuit(self):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use the example from the issue here, to have a proper regression test? The circuit_in used there differs from the one used here, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to the 4-gate example in the issue. Changed the comment for that test accordingly, as now there is no substitution happening, so the comment below this is not relevant anymore.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, my bad. Just checked that there is partial substitution happening, of course.

"""Test the template's global phase is respected (#14537)."""

template = QuantumCircuit(1)
template.h(0)
template.s(0)
template.h(0)
template.s(0)
template.h(0)
template.s(0)
template.global_phase = -np.pi / 4

qr = QuantumRegister(1, "qr")
circuit_in = QuantumCircuit(qr, global_phase=np.pi / 4)
circuit_in.h(qr[0])
circuit_in.s(qr[0])
circuit_in.h(qr[0])
circuit_in.s(qr[0])

result = TemplateOptimization([template])(circuit_in)

self.assertEqual(Operator(circuit_in), Operator(result))

def test_circuit_and_template_both_have_nonzero_global_phase(self):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests the same thing as the previous test, right? Do we need it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think given that the previous test was changed to 4 gates and therefore avoids the substitution is testing partial match, this test is now testing a different thing, which is the phase subtraction upon full substitution. So I would say we keep it?

"""Test that circuit and template global phases both contribute to the result (#14537)."""
template = QuantumCircuit(1)
template.h(0)
template.s(0)
template.h(0)
template.s(0)
template.h(0)
template.s(0)
template.global_phase = -np.pi / 4

qr = QuantumRegister(1, "qr")
circuit_in = QuantumCircuit(qr)
circuit_in.h(qr[0])
circuit_in.s(qr[0])
circuit_in.h(qr[0])
circuit_in.s(qr[0])
circuit_in.h(qr[0])
circuit_in.s(qr[0])
circuit_in.global_phase = np.pi / 3

result = TemplateOptimization([template])(circuit_in)

# All gates cancelled; total phase = circuit phase + template compensation
# = pi/3 + pi/4 = 7*pi/12.
self.assertAlmostEqual(result.global_phase, 7 * np.pi / 12)
self.assertEqual(result.count_ops(), {})
self.assertEqual(Operator(circuit_in), Operator(result))


if __name__ == "__main__":
unittest.main()