Skip to content

Commit

Permalink
Update basis equivalence test to be more specific (#6532)
Browse files Browse the repository at this point in the history
This commit updates the basis translator test:
test_skip_target_basis_equivalences_1 to be more speific to the case it
was designed to test. It was originally added in #6369 as a dedicated
reproduce test to validate we fixed issue #6350 where the basis
translator was trying to translate a gate already in the basis set which
resulted in a less than ideal (but still valid) output circuit. However,
this test was written to just mirror the original issue which was part
of a larger workflow and is too broad for a dedicated test case. This is
primarily because it was running a full transpile against a backend with
the sabre swap routing pass instead of only running the basis
translator (which is what we're testing). To fix this issue with the
test this commit extracts the qasm from the transpilation into the basis
translator when running the full reproduce from the original issue and
uses that as the input circuit, then the test is updated to only run
basis translation and optimization instead of the full transpilation
against a backend. This makes the test more targetted to what it's
specifically for and stable against unrelated changes to the sabre swap
pass.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
mtreinish and mergify[bot] authored Jun 11, 2021
1 parent 4c15d9e commit 4b27c63
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 30 deletions.
99 changes: 99 additions & 0 deletions test/python/qasm/TestBasisTranslator_skip_target.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
OPENQASM 2.0;
include "qelib1.inc";
qreg q[5];
h q[0];
cu1(4.1564508) q[2],q[1];
swap q[0],q[1];
swap q[1],q[2];
cx q[3],q[2];
tdg q[2];
swap q[3],q[4];
cx q[3],q[2];
t q[2];
swap q[4],q[3];
cx q[3],q[2];
tdg q[2];
t q[3];
swap q[3],q[4];
cx q[3],q[2];
t q[2];
h q[2];
cx q[3],q[4];
t q[3];
tdg q[4];
cx q[3],q[4];
swap q[2],q[3];
rzz(0.48622471) q[2],q[1];
ch q[1],q[2];
t q[1];
swap q[0],q[1];
id q[2];
swap q[1],q[2];
h q[4];
cx q[3],q[4];
swap q[3],q[2];
tdg q[4];
cx q[3],q[4];
swap q[2],q[3];
t q[4];
cx q[3],q[4];
t q[3];
swap q[3],q[2];
tdg q[4];
cx q[3],q[4];
cx q[3],q[2];
tdg q[2];
t q[3];
cx q[3],q[2];
y q[3];
t q[3];
h q[3];
swap q[2],q[3];
t q[4];
h q[4];
cu3(2.5787688,1.3265772,3.1398664) q[4],q[3];
tdg q[3];
cx q[3],q[2];
tdg q[2];
swap q[2],q[1];
cx q[0],q[1];
t q[1];
swap q[3],q[2];
cx q[2],q[1];
tdg q[1];
cx q[0],q[1];
t q[1];
h q[1];
t q[2];
swap q[2],q[1];
cx q[0],q[1];
t q[0];
tdg q[1];
cx q[0],q[1];
u3(4.8520889,3.6025647,5.7475542) q[0];
rz(2.1885681) q[0];
cx q[1],q[2];
h q[1];
cx q[2],q[1];
tdg q[1];
swap q[1],q[2];
y q[4];
cz q[3],q[4];
cx q[3],q[2];
t q[2];
cx q[1],q[2];
t q[1];
tdg q[2];
cx q[3],q[2];
t q[2];
h q[2];
swap q[2],q[3];
cx q[2],q[1];
tdg q[1];
t q[2];
cx q[2],q[1];
swap q[1],q[2];
cx q[3],q[2];
cu3(2.7675189,4.5725211,2.9940136) q[1],q[2];
rx(5.976651) q[4];
cx q[4],q[3];
37 changes: 7 additions & 30 deletions test/python/transpiler/test_basis_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


"""Test the BasisTranslator pass"""

import os

from numpy import pi

Expand All @@ -26,7 +26,6 @@
from qiskit.quantum_info import Operator
from qiskit.transpiler.exceptions import TranspilerError
from qiskit.transpiler.passes.basis import BasisTranslator, UnrollCustomDefinitions
from qiskit.test.mock import FakeAthens


from qiskit.circuit.library.standard_gates.equivalence_library import (
Expand Down Expand Up @@ -820,38 +819,16 @@ def test_condition_set_substitute_node(self):

def test_skip_target_basis_equivalences_1(self):
"""Test that BasisTranslator skips gates in the target_basis - #6085"""

qstr = 'OPENQASM 2.0; \
include "qelib1.inc"; \
qreg q[5]; \
cu1(4.1564508) q[2],q[3]; \
ccx q[0],q[1],q[4]; \
rzz(0.48622471) q[0],q[2]; \
ccx q[3],q[4],q[1]; \
ch q[2],q[0]; \
y q[3]; \
cu3(2.5787688,1.3265772,3.1398664) q[1],q[4]; \
id q[0]; \
y q[1]; \
t q[2]; \
t q[3]; \
tdg q[4]; \
cz q[0],q[1]; \
ccx q[2],q[4],q[3]; \
rx(5.976651) q[1]; \
u3(4.8520889,3.6025647,5.7475542) q[2]; \
cswap q[0],q[3],q[4]; \
rz(2.1885681) q[2]; \
cu3(2.7675189,4.5725211,2.9940136) q[0],q[3]; \
cx q[1],q[4];'
circ = QuantumCircuit()
circ = circ.from_qasm_str(qstr)

qasm_file = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"qasm",
"TestBasisTranslator_skip_target.qasm",
)
circ = circ.from_qasm_file(qasm_file)
circ_transpiled = transpile(
circ,
backend=FakeAthens(),
basis_gates=["id", "rz", "sx", "x", "cx"],
routing_method="sabre",
seed_transpiler=42,
)
self.assertEqual(circ_transpiled.count_ops(), {"cx": 91, "rz": 66, "sx": 22})

0 comments on commit 4b27c63

Please sign in to comment.