Beispiel #1
0
 def _circuit_zyz(theta, phi, lam, simplify=True, atol=DEFAULT_ATOL):
     circuit = QuantumCircuit(1)
     if simplify and np.isclose(theta, 0.0, atol=atol):
         circuit.append(RZGate(phi + lam), [0])
         return circuit
     if not simplify or not np.isclose(lam, 0.0, atol=atol):
         circuit.append(RZGate(lam), [0])
     if not simplify or not np.isclose(theta, 0.0, atol=atol):
         circuit.append(RYGate(theta), [0])
     if not simplify or not np.isclose(phi, 0.0, atol=atol):
         circuit.append(RZGate(phi), [0])
     return circuit
Beispiel #2
0
    def test_is_identity(self):
        """ The is_identity function determines whether a pair of gates
            forms the identity, when ignoring control qubits.
        """
        seq = [
            DAGNode({
                'type': 'op',
                'op': XGate().control()
            }),
            DAGNode({
                'type': 'op',
                'op': XGate().control(2)
            })
        ]
        self.assertTrue(HoareOptimizer()._is_identity(seq))

        seq = [
            DAGNode({
                'type': 'op',
                'op': RZGate(-pi / 2).control()
            }),
            DAGNode({
                'type': 'op',
                'op': RZGate(pi / 2).control(2)
            })
        ]
        self.assertTrue(HoareOptimizer()._is_identity(seq))

        seq = [
            DAGNode({
                'type': 'op',
                'op': CSwapGate()
            }),
            DAGNode({
                'type': 'op',
                'op': SwapGate()
            })
        ]
        self.assertTrue(HoareOptimizer()._is_identity(seq))

        seq = [
            DAGNode({
                'type': 'op',
                'op': UnitaryGate([[1, 0], [0, 1j]]).control()
            }),
            DAGNode({
                'type': 'op',
                'op': UnitaryGate([[1, 0], [0, -1j]])
            })
        ]
 def test_gate_multiplicity_binding(self):
     """Test binding when circuit contains multiple references to same gate"""
     from qiskit.extensions.standard import RZGate
     qc = QuantumCircuit(1)
     theta = Parameter('theta')
     gate = RZGate(theta)
     qc.append(gate, [0], [])
     qc.append(gate, [0], [])
     qc2 = qc.bind_parameters({theta: 1.0})
     self.assertEqual(len(qc2._parameter_table), 0)
     for gate, _, _ in qc2.data:
         self.assertEqual(float(gate.params[0]), 1.0)
class TestParameterCtrlState(QiskitTestCase):
    """Test gate equality with ctrl_state parameter."""
    @data((RXGate(0.5), CRXGate(0.5)), (RYGate(0.5), CRYGate(0.5)),
          (RZGate(0.5), CRZGate(0.5)), (XGate(), CXGate()),
          (YGate(), CYGate()), (ZGate(), CZGate()),
          (U1Gate(0.5), CU1Gate(0.5)), (SwapGate(), CSwapGate()),
          (HGate(), CHGate()), (U3Gate(0.1, 0.2, 0.3), CU3Gate(0.1, 0.2, 0.3)))
    @unpack
    def test_ctrl_state_one(self, gate, controlled_gate):
        """Test controlled gates with ctrl_state
        See https://github.com/Qiskit/qiskit-terra/pull/4025
        """
        self.assertEqual(gate.control(1, ctrl_state='1'), controlled_gate)
Beispiel #5
0
 def test_gate_multiplicity_binding(self):
     """Test binding when circuit contains multiple references to same gate"""
     from qiskit.extensions.standard import RZGate
     qc = QuantumCircuit(1)
     theta = Parameter('theta')
     gate = RZGate(theta)
     qc.append(gate, [0], [])
     qc.append(gate, [0], [])
     # test for both `bind_parameters` and `assign_parameters`
     for assign_fun in ['bind_parameters', 'assign_parameters']:
         with self.subTest(assign_fun=assign_fun):
             qc2 = getattr(qc, assign_fun)({theta: 1.0})
             self.assertEqual(len(qc2._parameter_table), 0)
             for gate, _, _ in qc2.data:
                 self.assertEqual(float(gate.params[0]), 1.0)
Beispiel #6
0
 def test_controlled_rz(self):
     """Test creation of controlled rz gate"""
     theta = 0.5
     self.assertEqual(RZGate(theta).control(), CrzGate(theta))
 def test_controlled_rz(self):
     """Test the creation of a controlled RZ gate."""
     theta = 0.5
     self.assertEqual(RZGate(theta).control(), CRZGate(theta))