예제 #1
0
    def test_2_qubit_identity_relations(self):
        """Tests identity relations for 2-qubit gates"""

        for gate_name in ("cx", "cz", "swap"):
            for qubits in ([0, 1], [1, 0]):
                with self.subTest(msg=f"append gate {gate_name} {qubits}"):
                    cliff = Clifford(np.eye(4))
                    cliff1 = cliff.copy()
                    cliff = _append_circuit(cliff, gate_name, qubits)
                    cliff = _append_circuit(cliff, gate_name, qubits)
                    self.assertEqual(cliff, cliff1)
예제 #2
0
    def test_append_2_qubit_gate(self, gate_name, qubits):
        """Tests for append of 2-qubit gate {gate_name} {qubits}."""

        targets_cliffords = {
            "cx [0, 1]":
            Clifford([[True, True, False, False], [False, True, False, False],
                      [False, False, True, False], [False, False, True,
                                                    True]]),
            "cx [1, 0]":
            Clifford([[True, False, False, False], [True, True, False, False],
                      [False, False, True, True], [False, False, False,
                                                   True]]),
            "cz [0, 1]":
            Clifford([[True, False, False, True], [False, True, True, False],
                      [False, False, True, False], [False, False, False,
                                                    True]]),
            "cz [1, 0]":
            Clifford([[True, False, False, True], [False, True, True, False],
                      [False, False, True, False], [False, False, False,
                                                    True]]),
            "swap [0, 1]":
            Clifford([[False, True, False, False], [True, False, False, False],
                      [False, False, False, True], [False, False, True,
                                                    False]]),
            "swap [1, 0]":
            Clifford([[False, True, False, False], [True, False, False, False],
                      [False, False, False, True], [False, False, True,
                                                    False]])
        }

        gate_qubits = gate_name + " " + str(qubits)
        cliff = _append_circuit(Clifford(np.eye(4)), gate_name, qubits)
        target = targets_cliffords[gate_qubits]
        self.assertEqual(target, cliff)
예제 #3
0
    def test_1_qubit_mult_relations(self):
        """Tests multiplicity relations for 1-qubit gates"""

        rels = [
            'x * y = z', 'x * z = y', 'y * z = x', 's * s = z',
            'sdg * sdg = z', 'sinv * sinv = z', 'sdg * h = v', 'h * s = w'
        ]

        for rel in rels:
            with self.subTest(msg='relation %s' % rel):
                split_rel = rel.split()
                cliff = Clifford([[1, 0], [0, 1]])
                cliff1 = cliff.copy()
                cliff = _append_circuit(cliff, split_rel[0], [0])
                cliff = _append_circuit(cliff, split_rel[2], [0])
                cliff1 = _append_circuit(cliff1, split_rel[4], [0])
                self.assertEqual(cliff, cliff1)
예제 #4
0
    def test_1_qubit_conj_relations(self):
        """Tests conjugation relations for 1-qubit gates"""

        rels = [
            'h * x * h = z', 'h * y * h = y', 's * x * sdg = y',
            'w * x * v = y', 'w * y * v = z', 'w * z * v = x'
        ]

        for rel in rels:
            with self.subTest(msg='relation %s' % rel):
                split_rel = rel.split()
                cliff = Clifford([[1, 0], [0, 1]])
                cliff1 = cliff.copy()
                cliff = _append_circuit(cliff, split_rel[0], [0])
                cliff = _append_circuit(cliff, split_rel[2], [0])
                cliff = _append_circuit(cliff, split_rel[4], [0])
                cliff1 = _append_circuit(cliff1, split_rel[6], [0])
                self.assertEqual(cliff, cliff1)
예제 #5
0
    def test_1_qubit_identity_relations(self):
        """Tests identity relations for 1-qubit gates"""

        for gate_name in ("x", "y", "z", "h"):
            with self.subTest(msg='identity for gate %s' % gate_name):
                cliff = Clifford([[1, 0], [0, 1]])
                cliff1 = cliff.copy()
                cliff = _append_circuit(cliff, gate_name, [0])
                cliff = _append_circuit(cliff, gate_name, [0])
                self.assertEqual(cliff, cliff1)

        gates = ['s', 's', 'v']
        inv_gates = ['sdg', 'sinv', 'w']

        for gate_name, inv_gate in zip(gates, inv_gates):
            with self.subTest(msg='identity for gate %s' % gate_name):
                cliff = Clifford([[1, 0], [0, 1]])
                cliff1 = cliff.copy()
                cliff = _append_circuit(cliff, gate_name, [0])
                cliff = _append_circuit(cliff, inv_gate, [0])
                self.assertEqual(cliff, cliff1)
예제 #6
0
    def test_1_qubit_mult_relations(self):
        """Tests multiplicity relations for 1-qubit gates"""

        rels = [
            "x * y = z",
            "x * z = y",
            "y * z = x",
            "s * s = z",
            "sdg * sdg = z",
            "sinv * sinv = z",
            "sdg * h = v",
            "h * s = w",
        ]

        for rel in rels:
            with self.subTest(msg="relation %s" % rel):
                split_rel = rel.split()
                cliff = Clifford([[1, 0], [0, 1]])
                cliff1 = cliff.copy()
                cliff = _append_circuit(cliff, split_rel[0], [0])
                cliff = _append_circuit(cliff, split_rel[2], [0])
                cliff1 = _append_circuit(cliff1, split_rel[4], [0])
                self.assertEqual(cliff, cliff1)
예제 #7
0
    def test_2_qubit_relations(self):
        """Tests relations for 2-qubit gates"""

        with self.subTest(msg='relation between cx, h and cz'):
            cliff = Clifford(np.eye(4))
            cliff1 = cliff.copy()
            cliff = _append_circuit(cliff, 'h', [1])
            cliff = _append_circuit(cliff, 'cx', [0, 1])
            cliff = _append_circuit(cliff, 'h', [1])
            cliff = _append_circuit(cliff, 'cz', [0, 1])
            self.assertEqual(cliff, cliff1)

        with self.subTest(msg='relation between cx and swap'):
            cliff = Clifford(np.eye(4))
            cliff1 = cliff.copy()
            cliff = _append_circuit(cliff, 'cx', [0, 1])
            cliff = _append_circuit(cliff, 'cx', [1, 0])
            cliff = _append_circuit(cliff, 'cx', [0, 1])
            cliff = _append_circuit(cliff, 'swap', [0, 1])
            self.assertEqual(cliff, cliff1)

        with self.subTest(msg='relation between cx and x'):
            cliff = Clifford(np.eye(4))
            cliff1 = cliff.copy()
            cliff = _append_circuit(cliff, 'cx', [0, 1])
            cliff = _append_circuit(cliff, 'x', [0])
            cliff = _append_circuit(cliff, 'cx', [0, 1])
            cliff = _append_circuit(cliff, 'x', [0])
            cliff = _append_circuit(cliff, 'x', [1])
            self.assertEqual(cliff, cliff1)

        with self.subTest(msg='relation between cx and z'):
            cliff = Clifford(np.eye(4))
            cliff1 = cliff.copy()
            cliff = _append_circuit(cliff, 'cx', [0, 1])
            cliff = _append_circuit(cliff, 'z', [1])
            cliff = _append_circuit(cliff, 'cx', [0, 1])
            cliff = _append_circuit(cliff, 'z', [0])
            cliff = _append_circuit(cliff, 'z', [1])
            self.assertEqual(cliff, cliff1)

        with self.subTest(msg='relation between cx and s'):
            cliff = Clifford(np.eye(4))
            cliff1 = cliff.copy()
            cliff = _append_circuit(cliff, 'cx', [1, 0])
            cliff = _append_circuit(cliff, 'cx', [0, 1])
            cliff = _append_circuit(cliff, 's', [1])
            cliff = _append_circuit(cliff, 'cx', [0, 1])
            cliff = _append_circuit(cliff, 'cx', [1, 0])
            cliff = _append_circuit(cliff, 'sdg', [0])
            self.assertEqual(cliff, cliff1)
예제 #8
0
    def test_append_1_qubit_gate(self):
        """Tests for append of 1-qubit gates"""

        target_table = {
            "i": np.array([[[True, False], [False, True]]], dtype=bool),
            "id": np.array([[[True, False], [False, True]]], dtype=bool),
            "iden": np.array([[[True, False], [False, True]]], dtype=bool),
            "x": np.array([[[True, False], [False, True]]], dtype=bool),
            "y": np.array([[[True, False], [False, True]]], dtype=bool),
            "z": np.array([[[True, False], [False, True]]], dtype=bool),
            "h": np.array([[[False, True], [True, False]]], dtype=bool),
            "s": np.array([[[True, True], [False, True]]], dtype=bool),
            "sdg": np.array([[[True, True], [False, True]]], dtype=bool),
            "sinv": np.array([[[True, True], [False, True]]], dtype=bool),
            "v": np.array([[[True, True], [True, False]]], dtype=bool),
            "w": np.array([[[False, True], [True, True]]], dtype=bool),
        }

        target_phase = {
            "i": np.array([[False, False]], dtype=bool),
            "id": np.array([[False, False]], dtype=bool),
            "iden": np.array([[False, False]], dtype=bool),
            "x": np.array([[False, True]], dtype=bool),
            "y": np.array([[True, True]], dtype=bool),
            "z": np.array([[True, False]], dtype=bool),
            "h": np.array([[False, False]], dtype=bool),
            "s": np.array([[False, False]], dtype=bool),
            "sdg": np.array([[True, False]], dtype=bool),
            "sinv": np.array([[True, False]], dtype=bool),
            "v": np.array([[False, False]], dtype=bool),
            "w": np.array([[False, False]], dtype=bool)
        }

        target_stabilizer = {
            "i": "+Z",
            "id": "+Z",
            "iden": "+Z",
            "x": "-Z",
            "y": "-Z",
            "z": "+Z",
            "h": "+X",
            "s": "+Z",
            "sdg": "+Z",
            "sinv": "+Z",
            "v": "+X",
            "w": "+Y",
        }

        target_destabilizer = {
            "i": "+X",
            "id": "+X",
            "iden": "+X",
            "x": "+X",
            "y": "-X",
            "z": "-X",
            "h": "+Z",
            "s": "+Y",
            "sdg": "-Y",
            "sinv": "-Y",
            "v": "+Y",
            "w": "+Z",
        }

        for gate_name in ("i", "id", "iden", "x", "y", "z", "h", "s", "sdg",
                          "v", "w"):
            with self.subTest(msg='append gate %s' % gate_name):
                cliff = Clifford([[1, 0], [0, 1]])
                cliff = _append_circuit(cliff, gate_name, [0])
                value_table = cliff.table._array
                value_phase = cliff.table._phase
                value_stabilizer = cliff.stabilizer.to_labels()
                value_destabilizer = cliff.destabilizer.to_labels()
                self.assertTrue(
                    np.all(np.array(value_table == target_table[gate_name])))
                self.assertTrue(
                    np.all(np.array(value_phase == target_phase[gate_name])))
                self.assertTrue(
                    np.all(
                        np.array(value_stabilizer ==
                                 [target_stabilizer[gate_name]])))
                self.assertTrue(
                    np.all(
                        np.array(value_destabilizer ==
                                 [target_destabilizer[gate_name]])))
예제 #9
0
    def test_2_qubit_relations(self):
        """Tests relations for 2-qubit gates"""

        with self.subTest(msg="relation between cx, h and cz"):
            cliff = Clifford(np.eye(4))
            cliff1 = cliff.copy()
            cliff = _append_circuit(cliff, "h", [1])
            cliff = _append_circuit(cliff, "cx", [0, 1])
            cliff = _append_circuit(cliff, "h", [1])
            cliff = _append_circuit(cliff, "cz", [0, 1])
            self.assertEqual(cliff, cliff1)

        with self.subTest(msg="relation between cx and swap"):
            cliff = Clifford(np.eye(4))
            cliff1 = cliff.copy()
            cliff = _append_circuit(cliff, "cx", [0, 1])
            cliff = _append_circuit(cliff, "cx", [1, 0])
            cliff = _append_circuit(cliff, "cx", [0, 1])
            cliff = _append_circuit(cliff, "swap", [0, 1])
            self.assertEqual(cliff, cliff1)

        with self.subTest(msg="relation between cx and x"):
            cliff = Clifford(np.eye(4))
            cliff1 = cliff.copy()
            cliff = _append_circuit(cliff, "cx", [0, 1])
            cliff = _append_circuit(cliff, "x", [0])
            cliff = _append_circuit(cliff, "cx", [0, 1])
            cliff = _append_circuit(cliff, "x", [0])
            cliff = _append_circuit(cliff, "x", [1])
            self.assertEqual(cliff, cliff1)

        with self.subTest(msg="relation between cx and z"):
            cliff = Clifford(np.eye(4))
            cliff1 = cliff.copy()
            cliff = _append_circuit(cliff, "cx", [0, 1])
            cliff = _append_circuit(cliff, "z", [1])
            cliff = _append_circuit(cliff, "cx", [0, 1])
            cliff = _append_circuit(cliff, "z", [0])
            cliff = _append_circuit(cliff, "z", [1])
            self.assertEqual(cliff, cliff1)

        with self.subTest(msg="relation between cx and s"):
            cliff = Clifford(np.eye(4))
            cliff1 = cliff.copy()
            cliff = _append_circuit(cliff, "cx", [1, 0])
            cliff = _append_circuit(cliff, "cx", [0, 1])
            cliff = _append_circuit(cliff, "s", [1])
            cliff = _append_circuit(cliff, "cx", [0, 1])
            cliff = _append_circuit(cliff, "cx", [1, 0])
            cliff = _append_circuit(cliff, "sdg", [0])
            self.assertEqual(cliff, cliff1)