def test_transpile_respects_arg_constraints(self):
     """Test that transpile() respects a heterogenous basis."""
     # Test CX on wrong link
     qc = QuantumCircuit(2)
     qc.h(0)
     qc.cx(1, 0)
     tqc = transpile(qc, self.backend)
     self.assertTrue(Operator.from_circuit(tqc).equiv(qc))
     # Below is done to check we're decomposing cx(1, 0) with extra
     # rotations to correct for direction. However because of fp
     # differences between windows and other platforms the optimization
     # from the 1q optimization passes differ and the output gates
     # change (while still being equivalent). This relaxes the check to
     # still ensure it's valid but not so specific that it fails on windows
     self.assertEqual(tqc.count_ops().keys(), {"cx", "u"})
     self.assertEqual(tqc.count_ops()["cx"], 1)
     self.assertLessEqual(tqc.count_ops()["u"], 4)
     self.assertMatchesTargetConstraints(tqc, self.backend.target)
     # Test ECR on wrong link
     qc = QuantumCircuit(2)
     qc.h(0)
     qc.ecr(0, 1)
     tqc = transpile(qc, self.backend)
     self.assertTrue(Operator.from_circuit(tqc).equiv(qc))
     self.assertEqual(tqc.count_ops(), {"ecr": 1, "u": 4})
     self.assertMatchesTargetConstraints(tqc, self.backend.target)
 def test_5q_ghz(self, opt_level, gate, bidirectional):
     backend = FakeBackend5QV2(bidirectional)
     qc = QuantumCircuit(5)
     qc.h(0)
     getattr(qc, gate)(0, 1)
     getattr(qc, gate)(2, 1)
     getattr(qc, gate)(2, 3)
     getattr(qc, gate)(4, 3)
     tqc = transpile(qc, backend, optimization_level=opt_level)
     t_op = Operator.from_circuit(tqc)
     self.assertTrue(t_op.equiv(qc))
     self.assertMatchesTargetConstraints(tqc, backend.target)
 def test_transpile_relies_on_gate_direction(self):
     """Test that transpile() relies on gate direction pass for 2q."""
     qc = QuantumCircuit(2)
     qc.h(0)
     qc.ecr(0, 1)
     tqc = transpile(qc, self.backend)
     expected = QuantumCircuit(2)
     expected.u(0, 0, -math.pi, 0)
     expected.u(math.pi / 2, 0, 0, 1)
     expected.ecr(1, 0)
     expected.u(math.pi / 2, 0, -math.pi, 0)
     expected.u(math.pi / 2, 0, -math.pi, 1)
     self.assertTrue(Operator.from_circuit(tqc).equiv(qc))
     self.assertEqual(tqc.count_ops(), {"ecr": 1, "u": 4})
     self.assertMatchesTargetConstraints(tqc, self.backend.target)
 def test_transpile(self, opt_level):
     """Test that transpile() works with a BackendV2 backend."""
     qc = QuantumCircuit(2)
     qc.h(1)
     qc.cz(1, 0)
     with self.assertLogs("qiskit.providers.backend", level="WARN") as log:
         tqc = transpile(qc, self.backend, optimization_level=opt_level)
     self.assertEqual(
         log.output,
         [
             "WARNING:qiskit.providers.backend:This backend's operations: "
             "cx,ecr only apply to a subset of qubits. Using this property to "
             "get 'basis_gates' for the transpiler may potentially create "
             "invalid output"
         ],
     )
     self.assertTrue(Operator.from_circuit(tqc).equiv(qc))
     self.assertMatchesTargetConstraints(tqc, self.backend.target)