Ejemplo n.º 1
0
    def test_orbital_rotation_matrix(self, phi):
        """Tests that the OrbitalRotation operation calculates the correct matrix"""
        op = qml.OrbitalRotation(phi, wires=[0, 1, 2, 3])
        res = op.matrix
        exp = OrbitalRotation(phi)

        assert np.allclose(res, exp)
Ejemplo n.º 2
0
 def circuit(phi):
     qml.PauliX(wires=0)
     qml.PauliX(wires=1)
     qml.OrbitalRotation(phi, wires=[0, 1, 2, 3])
     qml.adjoint(qml.OrbitalRotation)(phi, wires=[0, 1, 2, 3])
     qml.PauliX(wires=0)
     qml.PauliX(wires=1)
     return qml.state()
Ejemplo n.º 3
0
    def expand(self):

        with qml.tape.QuantumTape() as tape:

            qml.BasisEmbedding(self.init_state, wires=self.wires)
            weight = self.parameters[0]

            for layer in range(self.n_layers):
                for idx, wires in enumerate(self.qwires):

                    if self.include_pi:
                        qml.OrbitalRotation(np.pi, wires=wires)

                    qml.DoubleExcitation(weight[layer][idx][0], wires=wires)
                    qml.OrbitalRotation(weight[layer][idx][1], wires=wires)

        return tape
Ejemplo n.º 4
0
    def test_orbital_rotation_generator(self, phi):
        """Tests that the OrbitalRotation operation calculates the correct generator"""
        op = qml.OrbitalRotation(phi, wires=[0, 1, 2, 3])
        g, a = op.generator

        res = expm(1j * a * g * phi)
        exp = OrbitalRotation(phi)

        assert np.allclose(res, exp)
Ejemplo n.º 5
0
    def test_orbital_rotation_decomp(self, phi):
        """Tests that the OrbitalRotation operation calculates the correct decomposition.

        The decomposition has already been expressed in terms of single-qubit rotations
        and CNOTs. For each term in the decomposition we need to construct the appropriate
        four-qubit tensor product matrix and then multiply them together.
        """
        decomp = qml.OrbitalRotation(phi, wires=[0, 1, 2, 3]).decompose()

        from functools import reduce

        # To compute the matrix for CX on an arbitrary number of qubits, use the fact that
        # CU  = |0><0| \otimes I + |1><1| \otimes U
        def cnot_four_qubits(wires):
            proj_0_term = [
                StateZeroProjector if idx == wires[0] else np.eye(2)
                for idx in range(4)
            ]

            proj_1_term = [np.eye(2) for idx in range(4)]
            proj_1_term[wires[0]] = StateOneProjector
            proj_1_term[wires[1]] = X

            proj_0_kron = reduce(np.kron, proj_0_term)
            proj_1_kron = reduce(np.kron, proj_1_term)

            return proj_0_kron + proj_1_kron

        # Inserts a single-qubit matrix into a four-qubit matrix at the right place
        def single_mat_four_qubits(mat, wire):
            individual_mats = [
                mat if idx == wire else np.eye(2) for idx in range(4)
            ]
            return reduce(np.kron, individual_mats)

        mats = []
        for i in reversed(decomp):
            # Single-qubit gate
            if len(i.wires.tolist()) == 1:
                mat = single_mat_four_qubits(i.matrix, i.wires.tolist()[0])
                mats.append(mat)
            # Two-qubit gate
            else:
                mat = cnot_four_qubits(i.wires.tolist())
                mats.append(mat)

        decomposed_matrix = np.linalg.multi_dot(mats)
        exp = OrbitalRotation(phi)

        assert np.allclose(decomposed_matrix, exp)
Ejemplo n.º 6
0
 def circuit(phi):
     qml.PauliX(wires=0)
     qml.PauliX(wires=1)
     qml.OrbitalRotation(phi, wires=[0, 1, 2, 3])
     return qml.expval(qml.PauliZ(0))
Ejemplo n.º 7
0
        assert np.allclose(phi_t.grad, np.sin(phi))


label_data = [
    (qml.SingleExcitation(1.2345, wires=(0, 1)), "G", "G\n(1.23)", "G\n(1)"),
    (qml.SingleExcitationMinus(1.2345,
                               wires=(0, 1)), "G₋", "G₋\n(1.23)", "G₋\n(1)"),
    (qml.SingleExcitationPlus(1.2345,
                              wires=(0, 1)), "G₊", "G₊\n(1.23)", "G₊\n(1)"),
    (qml.DoubleExcitation(2.3456,
                          wires=(0, 1, 2, 3)), "G²", "G²\n(2.35)", "G²\n(2)"),
    (qml.DoubleExcitationPlus(2.3456, wires=(0, 1, 2, 3)), "G²₊",
     "G²₊\n(2.35)", "G²₊\n(2)"),
    (qml.DoubleExcitationMinus(2.345, wires=(0, 1, 2, 3)), "G²₋",
     "G²₋\n(2.35)", "G²₋\n(2)"),
    (
        qml.OrbitalRotation(2.3456, wires=(0, 1, 2, 3)),
        "OrbitalRotation",
        "OrbitalRotation\n(2.35)",
        "OrbitalRotation\n(2)",
    ),
]


@pytest.mark.parametrize("op, label1, label2, label3", label_data)
def test_label_method(op, label1, label2, label3):
    """Test the label method for qchem operations."""
    assert op.label() == label1
    assert op.label(decimals=2) == label2
    assert op.label(decimals=0) == label3
Ejemplo n.º 8
0
    "QubitCarry":
    qml.QubitCarry(wires=[0, 1, 2, 3]),
    "QubitSum":
    qml.QubitSum(wires=[0, 1, 2]),
    "PauliRot":
    qml.PauliRot(0, "XXYY", wires=[0, 1, 2, 3]),
    "U1":
    qml.U1(0, wires=0),
    "U2":
    qml.U2(0, 0, wires=0),
    "U3":
    qml.U3(0, 0, 0, wires=0),
    "SISWAP":
    qml.SISWAP(wires=[0, 1]),
    "OrbitalRotation":
    qml.OrbitalRotation(0, wires=[0, 1, 2, 3]),
}

all_ops = ops.keys()

# All qubit operations should be available to test in the device test suite
all_available_ops = qml.ops._qubit__ops__.copy()  # pylint: disable=protected-access
all_available_ops.remove(
    "CPhase")  # CPhase is an alias of ControlledPhaseShift
all_available_ops.remove("SQISW")  # SQISW is an alias of SISWAP
all_available_ops.add(
    "QFT"
)  # QFT was recently moved to being a template, but let's keep it here

if not set(all_ops) == all_available_ops:
    raise ValueError(