Skip to content

Commit

Permalink
transpiler/1q_decomposition: fix circuit score for zero gate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
airwoodix committed Mar 1, 2023
1 parent 15b2715 commit de6e380
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def _error(circuit, target=None, qubit=None):
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 @@ -198,11 +198,10 @@ def _error(circuit, target=None, qubit=None):
]
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
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

0 comments on commit de6e380

Please sign in to comment.