Example #1
0
    def test_sgate_int(self, n):
        """Test Sgate.power(n) method with n as integer."""
        result = SGate().power(n)

        self.assertEqual(result.label, "s^%s" % n)
        self.assertIsInstance(result, UnitaryGate)
        self.assertEqual(Operator(result), Operator(SGate()).power(n))
Example #2
0
    def test_invariant2(self, n):
        """Test op^(n) * op^(-n) == I"""
        result = Operator(SGate()).power(n) & Operator(SGate()).power(-n)
        expected = Operator(eye(2))

        self.assertEqual(len(result.data), len(expected.data))
        self.assertEqual(result, expected)
Example #3
0
 def test_invariant1_int(self, n):
     """Test (op^(1/n))^(n) == op, integer n"""
     result = SGate().power(1 / n).power(n)
     self.assertEqual(result.label, "unitary^" + str(n))
     self.assertEqual(len(result.definition), 1)
     self.assertIsInstance(result, Gate)
     self.assertTrue(Operator(SGate()), Operator(result))
Example #4
0
    def test_direct_root(self, degree):
        """Test nth root"""
        result = SGate().power(1 / degree)

        self.assertEqual(result.label, 's^' + str(1 / degree))
        self.assertEqual(len(result.definition), 1)
        self.assertIsInstance(result, Gate)
        self.assertEqual(Operator(result).power(degree), Operator(SGate()))
Example #5
0
    def test_invariant2(self, n):
        """Test op^(n) * op^(-n) == I
        """
        result = Operator(SGate().power(n)) @ Operator(SGate().power(-n))
        expected = Operator(eye(2))

        self.assertEqual(len(result.data), len(expected.data))
        assert_array_almost_equal(result.data, expected.data)
Example #6
0
 def test_invariant1_int(self, n):
     """Test (op^(1/n))^(n) == op, integer n
     """
     result = SGate().power(1 / n).power(n)
     self.assertEqual(result.label, 'unitary^' + str(n))
     self.assertEqual(len(result.definition), 1)
     self.assertIsInstance(result, Gate)
     assert_allclose(SGate().to_matrix(),
                     result.definition[0][0].to_matrix())
Example #7
0
    def test_sgate_int(self, n):
        """Test Sgate.power(n) method with n as integer.
        """
        result = SGate().power(n)

        self.assertEqual(result.label, 's^%s' % n)
        self.assertIsInstance(result, UnitaryGate)
        assert_array_almost_equal(result.to_matrix(),
                                  matrix_power(SGate().to_matrix(), n))
Example #8
0
    def test_float_gt_one(self, exponent):
        """Test greater-than-one exponents"""
        result = SGate().power(exponent)

        self.assertEqual(result.label, "s^" + str(exponent))
        self.assertEqual(len(result.definition), 1)
        self.assertIsInstance(result, Gate)
        # SGate().to_matrix() is diagonal so `**` is equivalent.
        self.assertEqual(Operator(SGate().to_matrix() ** exponent), Operator(result))
Example #9
0
    def test_sgate_float(self, n):
        """Test Sgate.power(<float>) method.
        """
        result = SGate().power(n)

        expected = self.results[n]
        self.assertEqual(result.label, 's^%s' % n)
        self.assertIsInstance(result, UnitaryGate)
        assert_array_almost_equal(result.to_matrix(), expected)
Example #10
0
    def test_direct_root(self, degree):
        """Test nth root"""
        result = SGate().power(1 / degree)

        self.assertEqual(result.label, 's^' + str(1 / degree))
        self.assertEqual(len(result.definition), 1)
        self.assertIsInstance(result, Gate)
        assert_allclose(
            matrix_power(result.definition[0][0].to_matrix(), degree),
            SGate().to_matrix())
Example #11
0
    def test_float_gt_one(self, exponent):
        """Test greater-than-one exponents """
        result = SGate().power(exponent)

        self.assertEqual(result.label, 's^' + str(exponent))
        self.assertEqual(len(result.definition), 1)
        self.assertIsInstance(result, Gate)
        # SGate().to_matrix() is diagonal so `**` is equivalent.
        assert_allclose(SGate().to_matrix()**exponent,
                        result.definition[0][0].to_matrix())
    def test_standard_1Q_one(self):
        """Test standard gate.repeat(1) method.
        """
        qr = QuantumRegister(1, 'qr')
        expected_circ = QuantumCircuit(qr)
        expected_circ.append(SGate(), [qr[0]])
        expected = expected_circ.to_instruction()

        result = SGate().repeat(1)

        self.assertEqual(result.name, 's*1')
        self.assertEqual(result.definition, expected.definition)
        self.assertIsInstance(result, Gate)
Example #13
0
def generate_unitary_gate(gate_name: str) -> Gate:
    # Rx, Ry and Rz gates that look like 'Rx(pi/2)
    if gate_name[0] == 'R' and gate_name[2] == '(':
        angle = parse_angle(gate_name[3:-1])
        if gate_name[1] == 'x':
            return RXGate(angle)
        elif gate_name[1] == 'y':
            return RYGate(angle)
        elif gate_name[1] == 'z':
            return RZGate(angle)
    else:
        unitary_gates = {
            "X": XGate(),
            "Y": YGate(),
            "S": SGate(),
            "Z": ZGate(),
            "H": HGate(),
            "T": TGate(),
            "I": IGate(),
            "W": WGate(),
            "Rz1": RZGate(-3 * np.pi / 8),
            "Rz2": RZGate(np.pi / 2),
            "Ry1": RYGate(np.pi / 2)
        }
        return unitary_gates[gate_name]
Example #14
0
def test_generate_unitary_gate():
    assert generate_unitary_gate("X") == XGate()
    assert generate_unitary_gate("Y") == YGate()
    assert generate_unitary_gate("S") == SGate()
    assert generate_unitary_gate("Z") == ZGate()
    assert generate_unitary_gate("H") == HGate()
    assert generate_unitary_gate("T") == TGate()
    assert generate_unitary_gate("I") == IdGate()
    for i in np.arange(-10, -1, 0.1):
        for j in np.arange(1, 10, 0.1):
            # Rx Gates
            assert generate_unitary_gate(f'Rx(pi*{j})') == RXGate(np.pi * j)
            assert generate_unitary_gate(f'Rx({i}/ {j}*pi)') == RXGate(i / j *
                                                                       np.pi)
            assert generate_unitary_gate(f'Rx( {i}*{j}/pi )') == RXGate(i * j /
                                                                        np.pi)
            assert generate_unitary_gate(f'Rx( {i} / {j}/pi)') == RXGate(
                i / j / np.pi)
            # Ry Gates
            assert generate_unitary_gate(f'Ry(pi*{j})') == RYGate(np.pi * j)
            assert generate_unitary_gate(f'Ry({i}/ {j}*pi)') == RYGate(i / j *
                                                                       np.pi)
            assert generate_unitary_gate(f'Ry( {i}*{j}/pi )') == RYGate(i * j /
                                                                        np.pi)
            assert generate_unitary_gate(f'Ry( {i} / {j}/pi)') == RYGate(
                i / j / np.pi)
            # Rz Gates
            assert generate_unitary_gate(f'Rz(pi*{j})') == RZGate(np.pi * j)
            assert generate_unitary_gate(f'Rz({i}/ {j}*pi)') == RZGate(i / j *
                                                                       np.pi)
            assert generate_unitary_gate(f'Rz( {i}*{j}/pi )') == RZGate(i * j /
                                                                        np.pi)
            assert generate_unitary_gate(f'Rz( {i} / {j}/pi)') == RZGate(
                i / j / np.pi)
Example #15
0
    def test_sgate_float(self, n):
        """Test Sgate.power(<float>) method."""
        result = SGate().power(n)

        expected = self.results[n]
        self.assertEqual(result.label, "s^%s" % n)
        self.assertIsInstance(result, UnitaryGate)
        self.assertEqual(Operator(result), Operator(expected))
    def _gen_lookup_table(self):

        op1 = RZGate(-3 * np.pi / 8)
        op2 = RYGate(np.pi / 2)
        op3 = RZGate(np.pi / 2)
        result = {"X": XGate(), "Y": YGate(), "S": SGate(), "Z": ZGate(), "H": HGate(), "T": TGate(), "W": self._gen_w_gate(),
                  "Rz1": RZGate(-3 * np.pi / 8), "Rz2": RZGate(np.pi/2), "Ry1": RYGate(np.pi/2)}
        return result
Example #17
0
    def test_standard_sqrt(self):
        """Test standard Gate.power(1/2) method."""
        expected = array([[1, 0], [0, 0.70710678118 + 0.70710678118j]], dtype=complex)

        result = SGate().power(1 / 2)

        self.assertEqual(result.label, "s^0.5")
        self.assertEqual(len(result.definition), 1)
        self.assertIsInstance(result, Gate)
        self.assertEqual(Operator(result), Operator(expected))
Example #18
0
    def test_minus_zero_two(self, exponent=-0.2):
        """Test Sgate^(-0.2)"""
        result = SGate().power(exponent)

        self.assertEqual(result.label, 's^' + str(exponent))
        self.assertEqual(len(result.definition), 1)
        self.assertIsInstance(result, Gate)
        assert_allclose(
            array([[1, 0], [0, 0.95105652 - 0.30901699j]], dtype=complex),
            result.definition[0][0].to_matrix())
Example #19
0
    def test_minus_zero_two(self, exponent=-0.2):
        """Test Sgate^(-0.2)"""
        result = SGate().power(exponent)

        self.assertEqual(result.label, "s^" + str(exponent))
        self.assertEqual(len(result.definition), 1)
        self.assertIsInstance(result, Gate)
        self.assertEqual(
            Operator(array([[1, 0], [0, 0.95105652 - 0.30901699j]], dtype=complex)),
            Operator(result),
        )
Example #20
0
    def test_standard_sqrt(self):
        """Test standard Gate.power(1/2) method.
        """
        expected = array([[1, 0], [0, 0.70710678118 + 0.70710678118j]],
                         dtype=complex)

        result = SGate().power(1 / 2)

        self.assertEqual(result.label, 's^0.5')
        self.assertEqual(len(result.definition), 1)
        self.assertIsInstance(result, Gate)
        assert_allclose(result.definition[0][0].to_matrix(), expected)
    def test_unroller_one(self):
        """Test unrolling gate.repeat(1).
        """
        qr = QuantumRegister(1, 'qr')

        circuit = QuantumCircuit(qr)
        circuit.append(SGate().repeat(1), [qr[0]])
        result = PassManager(Unroller('u3')).run(circuit)

        expected = QuantumCircuit(qr)
        expected.append(U3Gate(0, 0, pi / 2), [qr[0]])

        self.assertEqual(result, expected)
 def test_standard_1Q_minus_one(self):
     """Test standard 2Q gate.repeat(-1) method. Raises, since n<1.
     """
     with self.assertRaises(CircuitError) as context:
         _ = SGate().repeat(-1)
     self.assertIn('strictly positive integer', str(context.exception))
Example #23
0
        }
        """
        definition = []
        q = QuantumRegister(1, "q")
        rule = [(RZGate(-3 * np.pi / 8), [q[0]], []),
                (RZGate(np.pi / 2), [q[0]], []),
                (RYGate(np.pi / 2), [q[0]], [])]
        for inst in rule:
            definition.append(inst)
        self.definition = definition


unitary_gates = {
    "X": XGate(),
    "Y": YGate(),
    "S": SGate(),
    "Z": ZGate(),
    "H": HGate(),
    "T": TGate(),
    "I": IdGate(),
    "W": WGate(),
    "Rz1": RZGate(-3 * np.pi / 8),
    "Rz2": RZGate(np.pi / 2),
    "Ry1": RYGate(np.pi / 2)
}


class Protocol(Enum):
    """
    The various different quantum/classical game theory game protocols
    """
 def test_noiseless_s_gate_standard_basis(self):
     basis = default_gateset_basis()
     basis.add_gate(SGate())
     self.run_test_on_basis_and_noise(gateset_basis=basis)
 def test_standard_no_int(self):
     """Test standard Gate.repeat(2/3) method. Raises, since n is not int.
     """
     with self.assertRaises(CircuitError) as context:
         _ = SGate().repeat(2 / 3)
     self.assertIn('strictly positive integer', str(context.exception))