Ejemplo n.º 1
0
def test_identity(backend):
    gatelist = [gates.H(0), gates.H(1), gates.I(0), gates.I(1)]
    final_state = apply_gates(gatelist, nqubits=2)
    target_state = np.ones_like(final_state) / 2.0
    K.assert_allclose(final_state, target_state)
    gatelist = [gates.H(0), gates.H(1), gates.I(0, 1)]
    final_state = apply_gates(gatelist, nqubits=2)
    K.assert_allclose(final_state, target_state)
Ejemplo n.º 2
0
def test_identity(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    gatelist = [gates.H(0), gates.H(1), gates.I(0), gates.I(1)]
    final_state = apply_gates(gatelist, nqubits=2)
    target_state = np.ones_like(final_state) / 2.0
    np.testing.assert_allclose(final_state, target_state)
    gatelist = [gates.H(0), gates.H(1), gates.I(0, 1)]
    final_state = apply_gates(gatelist, nqubits=2)
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
Ejemplo n.º 3
0
def test_identity_gate(backend, accelerators):
    """Check identity gate is working properly."""
    qibo.set_backend(backend)
    c = Circuit(2, accelerators)
    c.add((gates.H(i) for i in range(2)))
    c.add(gates.I(0))
    final_state = c.execute().numpy()
    target_state = np.ones_like(final_state) / 2.0
    np.testing.assert_allclose(final_state, target_state)

    c = Circuit(4, accelerators)
    c.add((gates.H(i) for i in range(4)))
    c.add(gates.I(0, 1))
    final_state = c.execute().numpy()
    target_state = np.ones_like(final_state) / 4.0
    np.testing.assert_allclose(final_state, target_state)
Ejemplo n.º 4
0
 def __matmul__(self, other: "Gate") -> "Gate":
     """Gate multiplication."""
     if self.qubits != other.qubits:
         raise_error(
             NotImplementedError, "Cannot multiply gates that target "
             "different qubits.")
     if self.__class__.__name__ == other.__class__.__name__:
         square_identity = {"H", "X", "Y", "Z", "CNOT", "CZ", "SWAP"}
         if self.__class__.__name__ in square_identity:
             from qibo import gates
             return gates.I(*self.qubits)
     return None
Ejemplo n.º 5
0
def test_singlequbit_gates_cirq(backend):
    c1 = Circuit(2)
    c1.add(gates.H(0))
    c1.add(gates.X(1))
    c1.add(gates.Y(0))
    c1.add(gates.Z(1))
    c1.add(gates.S(0))
    c1.add(gates.SDG(1))
    c1.add(gates.T(0))
    c1.add(gates.TDG(1))
    c1.add(gates.I(0))
    final_state_c1 = c1()

    c2 = circuit_from_qasm(c1.to_qasm())
    c2depth = len(cirq.Circuit(c2.all_operations()))
    assert c1.depth == c2depth
    final_state_c2 = cirq.Simulator().simulate(c2).final_state_vector  # pylint: disable=no-member
    np.testing.assert_allclose(final_state_c1, final_state_c2, atol=_atol)

    c3 = Circuit.from_qasm(c2.to_qasm())
    assert c3.depth == c2depth
    final_state_c3 = c3()
    np.testing.assert_allclose(final_state_c3, final_state_c2, atol=_atol)
Ejemplo n.º 6
0
    def calculate(self):
        """Calculates fused gate."""
        if not self.completed:
            raise_error(
                RuntimeError, "Cannot calculate fused gates for incomplete "
                "FusionGroup.")
        # Case 1: Special gate
        if self.special_gate is not None:
            assert not self.gates0[0] and not self.gates1[0]
            assert not self.two_qubit_gates
            return (self.special_gate, )

        # Case 2: Two-qubit gates only (no one-qubit gates)
        if self.first_gate(0) is None and self.first_gate(1) is None:
            assert self.two_qubit_gates
            # Case 2a: One two-qubit gate only
            if len(self.two_qubit_gates) == 1:
                return (self.two_qubit_gates[0], )

            # Case 2b: Two or more two-qubit gates
            fused_matrix = self._two_qubit_matrix(self.two_qubit_gates[0])
            for gate in self.two_qubit_gates[1:]:
                matrix = self._two_qubit_matrix(gate)
                fused_matrix = self.K.matmul(matrix, fused_matrix)
            return (gates.Unitary(fused_matrix, self.qubit0, self.qubit1), )

        # Case 3: One-qubit gates exist
        if not self.gates0[-1] and not self.gates1[-1]:
            self.gates0.pop()
            self.gates1.pop()

        # Fuse one-qubit gates
        ident0 = gates.I(self.qubit0)
        gates0 = (functools.reduce(operator.matmul, reversed(gates), ident0)
                  if len(gates) != 1 else gates[0] for gates in self.gates0)
        if self.qubit1 is not None:
            ident1 = gates.I(self.qubit1)
            gates1 = (functools.reduce(operator.matmul, reversed(gates),
                                       ident1) if len(gates) != 1 else gates[0]
                      for gates in self.gates1)

        # Case 3a: One-qubit gates only (no two-qubit gates)
        if not self.two_qubit_gates:
            gates0 = list(gates0)[::-1]
            fused_gate0 = (functools.reduce(operator.matmul, gates0, ident0)
                           if len(gates0) != 1 else gates0[0])
            if self.qubit1 is None:
                return (fused_gate0, )

            gates1 = list(gates1)[::-1]
            fused_gate1 = (functools.reduce(operator.matmul, gates1, ident1)
                           if len(gates1) != 1 else gates1[0])
            return (fused_gate0, fused_gate1)

        # Case 3b: One-qubit and two-qubit gates exist
        fused_matrix = self._one_qubit_matrix(next(gates0), next(gates1))
        for g0, g1, g2 in zip(gates0, gates1, self.two_qubit_gates):
            matrix = self._one_qubit_matrix(g0, g1)
            matrix2 = self._two_qubit_matrix(g2)
            fused_matrix = self.K.matmul(self.K.matmul(matrix, matrix2),
                                         fused_matrix)

        if len(self.two_qubit_gates) == len(self.gates0):
            g2 = self.two_qubit_gates[-1]
            if self.is_efficient(g2):
                fused_gate = gates.Unitary(fused_matrix, self.qubit0,
                                           self.qubit1)
                return (fused_gate, g2)

            matrix2 = self._two_qubit_matrix(g2)
            fused_matrix = self.K.matmul(matrix2, fused_matrix)

        fused_gate = gates.Unitary(fused_matrix, self.qubit0, self.qubit1)
        return (fused_gate, )