Skip to content

transpiler/1q_decomposition: fix circuit score for zero gate errors #9701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def _error(circuit, target, qubit):
if isinstance(circuit, list):
return len(circuit)
else:
return len(circuit._multi_graph) - 2
return circuit.size()
else:
if isinstance(circuit, list):
gate_fidelities = [
Expand All @@ -193,11 +193,10 @@ def _error(circuit, target, qubit):
]
gate_error = 1 - np.product(gate_fidelities)
if gate_error == 0.0:
# prefer shorter circuits among those with zero error
if isinstance(circuit, list):
return -100 + len(circuit)
else:
return -100 + len(
circuit._multi_graph
) # prefer shorter circuits among those with zero error
return -100 + circuit.size()
else:
return gate_error
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixes a bug in the :class:`Optimize1qGatesDecomposition` transformation pass
where the score for substitutions was wrongly calculated when the gate
errors are zero.
35 changes: 35 additions & 0 deletions test/python/transpiler/test_optimize_1q_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,41 @@ def test_euler_decomposition_zsx_2(self):
result = passmanager.run(circuit)
self.assertEqual(circuit, result, f"Circuit:\n{circuit}\nResult:\n{result}")

def test_optimize_run_of_u_to_single_u_on_target_no_error(self):
"""U(pi/3, 0, 0) * U(pi/3, 0, 0) * U(pi/3, 0, 0) -> U(pi, 0, 0)."""
qr = QuantumRegister(1, "qr")
circuit = QuantumCircuit(qr)
for _ in range(3):
circuit.append(UGate(np.pi / 3, 0.0, 0.0), [qr[0]])

expected = QuantumCircuit(qr)
expected.append(UGate(np.pi, 0.0, 0.0), [qr[0]])

passmanager = PassManager()
passmanager.append(Optimize1qGatesDecomposition(target=target_rz_ry_u_noerror))
result = passmanager.run(circuit)

msg = f"expected:\n{expected}\nresult:\n{result}"
self.assertEqual(expected, result, msg=msg)

def test_optimize_run_of_u_to_single_u_no_target(self):
"""U(pi/3, 0, 0) * U(pi/3, 0, 0) * U(pi/3, 0, 0) -> U(pi, 0, 0)."""
qr = QuantumRegister(1, "qr")
circuit = QuantumCircuit(qr)
for _ in range(3):
circuit.append(UGate(np.pi / 3, 0.0, 0.0), [qr[0]])

expected = QuantumCircuit(qr)
expected.append(UGate(np.pi, 0.0, 0.0), [qr[0]])

basis = ["u"]
passmanager = PassManager()
passmanager.append(Optimize1qGatesDecomposition(basis))
result = passmanager.run(circuit)

msg = f"expected:\n{expected}\nresult:\n{result}"
self.assertEqual(expected, result, msg=msg)

def test_optimize_u_to_phase_gate(self):
"""U(0, 0, pi/4) -> p(pi/4). Basis [p, sx]."""
qr = QuantumRegister(2, "qr")
Expand Down