コード例 #1
0
 def test_string_gate_error(self):
     """Test that when gate is passed as a string an error is raised."""
     qc = QuantumCircuit(2, 2)
     qc.h(0)
     qc.h(0)
     with self.assertRaises(TranspilerError):
         InverseCancellation(["h"])
コード例 #2
0
 def test_non_gate_inverse_raise_error(self):
     """Test that non-inverse gate inputs raise an error."""
     qc = QuantumCircuit(2, 2)
     qc.rx(np.pi / 4, 0)
     qc.rx(np.pi / 4, 0)
     with self.assertRaises(TranspilerError):
         InverseCancellation([(RXGate(np.pi / 4))])
コード例 #3
0
 def test_basic_gate_inverse(self):
     """Test that a basic pair of gate inverse can be cancelled."""
     qc = QuantumCircuit(2, 2)
     qc.rx(np.pi / 4, 0)
     qc.rx(-np.pi / 4, 0)
     pass_ = InverseCancellation([(RXGate(np.pi / 4), RXGate(-np.pi / 4))])
     pm = PassManager(pass_)
     new_circ = pm.run(qc)
     gates_after = new_circ.count_ops()
     self.assertNotIn("rx", gates_after)
コード例 #4
0
 def test_basic_cx_self_inverse(self):
     """Test that a single self-inverse cx gate as input can be cancelled."""
     qc = QuantumCircuit(2, 2)
     qc.cx(0, 1)
     qc.cx(0, 1)
     pass_ = InverseCancellation([CXGate()])
     pm = PassManager(pass_)
     new_circ = pm.run(qc)
     gates_after = new_circ.count_ops()
     self.assertNotIn("cx", gates_after)
コード例 #5
0
 def test_gate_inverse_phase_gate(self):
     """Test that an inverse pair of a PhaseGate can be cancelled."""
     qc = QuantumCircuit(2, 2)
     qc.p(np.pi / 4, 0)
     qc.p(-np.pi / 4, 0)
     pass_ = InverseCancellation([(PhaseGate(np.pi / 4), PhaseGate(-np.pi / 4))])
     pm = PassManager(pass_)
     new_circ = pm.run(qc)
     gates_after = new_circ.count_ops()
     self.assertNotIn("p", gates_after)
コード例 #6
0
 def test_non_inverse_do_not_cancel(self):
     """Test that non-inverse gate pairs do not cancel."""
     qc = QuantumCircuit(2, 2)
     qc.rx(np.pi / 4, 0)
     qc.rx(np.pi / 4, 0)
     pass_ = InverseCancellation([(RXGate(np.pi / 4), RXGate(-np.pi / 4))])
     pm = PassManager(pass_)
     new_circ = pm.run(qc)
     gates_after = new_circ.count_ops()
     self.assertIn("rx", gates_after)
     self.assertEqual(gates_after["rx"], 2)
コード例 #7
0
 def test_inverse_with_different_names(self):
     """Test that inverse gates that have different names."""
     qc = QuantumCircuit(2, 2)
     qc.t(0)
     qc.tdg(0)
     pass_ = InverseCancellation([(TGate(), TdgGate())])
     pm = PassManager(pass_)
     new_circ = pm.run(qc)
     gates_after = new_circ.count_ops()
     self.assertNotIn("t", gates_after)
     self.assertNotIn("tdg", gates_after)
コード例 #8
0
 def test_odd_number_self_inverse(self):
     """Test that an odd number of self-inverse gates leaves one gate remaining."""
     qc = QuantumCircuit(2, 2)
     qc.h(0)
     qc.h(0)
     qc.h(0)
     pass_ = InverseCancellation([HGate()])
     pm = PassManager(pass_)
     new_circ = pm.run(qc)
     gates_after = new_circ.count_ops()
     self.assertIn("h", gates_after)
     self.assertEqual(gates_after["h"], 1)
コード例 #9
0
 def test_cx_do_not_wrongly_cancel(self):
     """Test that CX(0,1) and CX(1, 0) do not cancel out, when (CX, CX) is passed
     as an inverse pair."""
     qc = QuantumCircuit(2, 0)
     qc.cx(0, 1)
     qc.cx(1, 0)
     pass_ = InverseCancellation([(CXGate(), CXGate())])
     pm = PassManager(pass_)
     new_circ = pm.run(qc)
     gates_after = new_circ.count_ops()
     self.assertIn("cx", gates_after)
     self.assertEqual(gates_after["cx"], 2)
コード例 #10
0
 def test_self_inverse_on_different_qubits(self):
     """Test that self_inverse gates cancel on the correct qubits."""
     qc = QuantumCircuit(2, 2)
     qc.h(0)
     qc.h(1)
     qc.h(0)
     qc.h(1)
     pass_ = InverseCancellation([HGate()])
     pm = PassManager(pass_)
     new_circ = pm.run(qc)
     gates_after = new_circ.count_ops()
     self.assertNotIn("h", gates_after)
コード例 #11
0
 def test_four_alternating_inverse_gates(self):
     """Test that inverse cancellation works correctly for alternating sequences
     of inverse gates of even-length."""
     qc = QuantumCircuit(2, 2)
     qc.p(np.pi / 4, 0)
     qc.p(-np.pi / 4, 0)
     qc.p(np.pi / 4, 0)
     qc.p(-np.pi / 4, 0)
     pass_ = InverseCancellation([(PhaseGate(np.pi / 4),
                                   PhaseGate(-np.pi / 4))])
     pm = PassManager(pass_)
     new_circ = pm.run(qc)
     gates_after = new_circ.count_ops()
     self.assertNotIn("p", gates_after)
コード例 #12
0
 def test_non_consecutive_gates(self):
     """Test that only consecutive gates cancel."""
     qc = QuantumCircuit(2, 2)
     qc.h(0)
     qc.h(0)
     qc.h(0)
     qc.cx(0, 1)
     qc.cx(0, 1)
     qc.h(0)
     pass_ = InverseCancellation([HGate(), CXGate()])
     pm = PassManager(pass_)
     new_circ = pm.run(qc)
     gates_after = new_circ.count_ops()
     self.assertNotIn("cx", gates_after)
     self.assertEqual(gates_after["h"], 2)
コード例 #13
0
 def test_sequence_of_inverse_gates_1(self):
     """Test that inverse cancellation works correctly for more general sequences
     of inverse gates. In this test two pairs of inverse gates are supposed to
     cancel out."""
     qc = QuantumCircuit(2, 2)
     qc.p(np.pi / 4, 0)
     qc.p(-np.pi / 4, 0)
     qc.p(-np.pi / 4, 0)
     qc.p(np.pi / 4, 0)
     qc.p(np.pi / 4, 0)
     pass_ = InverseCancellation([(PhaseGate(np.pi / 4),
                                   PhaseGate(-np.pi / 4))])
     pm = PassManager(pass_)
     new_circ = pm.run(qc)
     gates_after = new_circ.count_ops()
     self.assertIn("p", gates_after)
     self.assertEqual(gates_after["p"], 1)
コード例 #14
0
 def test_sequence_of_inverse_gates_2(self):
     """Test that inverse cancellation works correctly for more general sequences
     of inverse gates. In this test, in theory three pairs of inverse gates can
     cancel out, but in practice only two pairs are back-to-back."""
     qc = QuantumCircuit(2, 2)
     qc.p(np.pi / 4, 0)
     qc.p(np.pi / 4, 0)
     qc.p(-np.pi / 4, 0)
     qc.p(-np.pi / 4, 0)
     qc.p(-np.pi / 4, 0)
     qc.p(np.pi / 4, 0)
     qc.p(np.pi / 4, 0)
     pass_ = InverseCancellation([(PhaseGate(np.pi / 4),
                                   PhaseGate(-np.pi / 4))])
     pm = PassManager(pass_)
     new_circ = pm.run(qc)
     gates_after = new_circ.count_ops()
     self.assertIn("p", gates_after)
     self.assertEqual(gates_after["p"] % 2, 1)