def setUp(self):
        super().setUp()

        self.devices = [DefaultQubit(wires=self.num_subsystems)]
        if self.args.device == 'simulator' or self.args.device == 'all':
            self.devices.append(
                ProjectQSimulator(wires=self.num_subsystems, verbose=True))
            self.devices.append(
                ProjectQSimulator(wires=self.num_subsystems,
                                  shots=20000000,
                                  verbose=True))
        if self.args.device == 'ibm' or self.args.device == 'all':
            ibm_options = pennylane.default_config['projectq.ibm']
            if "user" in ibm_options and "password" in ibm_options:
                self.devices.append(
                    ProjectQIBMBackend(wires=self.num_subsystems,
                                       use_hardware=False,
                                       num_runs=8 * 1024,
                                       user=ibm_options['user'],
                                       password=ibm_options['password'],
                                       verbose=True))
            else:
                log.warning(
                    "Skipping test of the ProjectQIBMBackend device because IBM login credentials could not be found in the PennyLane configuration file."
                )
        if self.args.device == 'classical' or self.args.device == 'all':
            self.devices.append(
                ProjectQClassicalSimulator(wires=self.num_subsystems,
                                           verbose=True))
    def test_expand_one(self):
        """Test that a 1 qubit gate correctly expands to 3 qubits."""
        self.logTestName()

        dev = DefaultQubit(wires=3)

        # test applied to wire 0
        res = dev.expand_one(U, [0])
        expected = np.kron(np.kron(U, I), I)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test applied to wire 1
        res = dev.expand_one(U, [1])
        expected = np.kron(np.kron(I, U), I)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test applied to wire 2
        res = dev.expand_one(U, [2])
        expected = np.kron(np.kron(I, I), U)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test exception raised if U is not 2x2 matrix
        with self.assertRaisesRegex(ValueError, "2x2 matrix required"):
            dev.expand_one(U2, [0])

        # test exception raised if more than one subsystem provided
        with self.assertRaisesRegex(ValueError,
                                    "One target subsystem required"):
            dev.expand_one(U, [0, 1])
    def test_expand_two(self):
        """Test that a 2 qubit gate correctly expands to 3 qubits."""
        self.logTestName()

        dev = DefaultQubit(wires=4)

        # test applied to wire 0+1
        res = dev.expand(U2, [0, 1])
        expected = np.kron(np.kron(U2, I), I)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test applied to wire 1+2
        res = dev.expand(U2, [1, 2])
        expected = np.kron(np.kron(I, U2), I)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test applied to wire 2+3
        res = dev.expand(U2, [2, 3])
        expected = np.kron(np.kron(I, I), U2)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # CNOT with target on wire 1
        res = dev.expand(CNOT, [1, 0])
        rows = np.array([0, 2, 1, 3])
        expected = np.kron(np.kron(CNOT[:, rows][rows], I), I)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test exception raised if unphysical subsystems provided
        with self.assertRaisesRegex(ValueError, "Invalid target subsystems provided in 'wires' argument"):
            dev.expand(U2, [-1, 5])
    def test_expand_one(self):
        """Test that a 1 qubit gate correctly expands to 3 qubits."""
        self.logTestName()

        dev = DefaultQubit(wires=3)

        # test applied to wire 0
        res = dev.expand(U, [0])
        expected = np.kron(np.kron(U, I), I)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test applied to wire 1
        res = dev.expand(U, [1])
        expected = np.kron(np.kron(I, U), I)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test applied to wire 2
        res = dev.expand(U, [2])
        expected = np.kron(np.kron(I, I), U)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)
    def setUp(self):
        super().setUp()

        self.devices = [DefaultQubit(wires=self.num_subsystems)]
        if self.args.provider == 'basicaer' or self.args.provider == 'all':
            self.devices.append(
                BasicAerQiskitDevice(wires=self.num_subsystems))
        if self.args.provider == 'aer' or self.args.provider == 'all':
            self.devices.append(AerQiskitDevice(wires=self.num_subsystems))
        if self.args.provider == 'legacy' or self.args.provider == 'all':
            self.devices.append(
                LegacySimulatorsQiskitDevice(wires=self.num_subsystems))
        if self.args.provider == 'ibm' or self.args.provider == 'all':
            if IBMQX_TOKEN is not None:
                self.devices.append(
                    IbmQQiskitDevice(wires=self.num_subsystems,
                                     num_runs=8 * 1024,
                                     ibmqx_token=IBMQX_TOKEN))
            else:
                log.warning(
                    "Skipping test of the IbmQQiskitDevice device because IBM login credentials could not be found in the PennyLane configuration file."
                )
    def test_expand_three(self):
        """Test that a 3 qubit gate correctly expands to 4 qubits."""
        self.logTestName()

        dev = DefaultQubit(wires=4)

        # test applied to wire 0,1,2
        res = dev.expand(U_toffoli, [0, 1, 2])
        expected = np.kron(U_toffoli, I)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test applied to wire 1,2,3
        res = dev.expand(U_toffoli, [1, 2, 3])
        expected = np.kron(I, U_toffoli)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test applied to wire 0,2,3
        res = dev.expand(U_toffoli, [0, 2, 3])
        expected = np.kron(U_swap, np.kron(I, I)) @ np.kron(I, U_toffoli) @ np.kron(U_swap, np.kron(I, I))
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test applied to wire 0,1,3
        res = dev.expand(U_toffoli, [0, 1, 3])
        expected = np.kron(np.kron(I, I), U_swap) @ np.kron(U_toffoli, I) @ np.kron(np.kron(I, I), U_swap)
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test applied to wire 3, 1, 2
        res = dev.expand(U_toffoli, [3, 1, 2])
        # change the control qubit on the Toffoli gate
        rows = np.array([0, 4, 1, 5, 2, 6, 3, 7])
        expected = np.kron(I, U_toffoli[:, rows][rows])
        self.assertAllAlmostEqual(res, expected, delta=self.tol)

        # test applied to wire 3, 0, 2
        res = dev.expand(U_toffoli, [3, 0, 2])
        # change the control qubit on the Toffoli gate
        rows = np.array([0, 4, 1, 5, 2, 6, 3, 7])
        expected = np.kron(U_swap, np.kron(I, I)) @ np.kron(I, U_toffoli[:, rows][rows]) @ np.kron(U_swap, np.kron(I, I))
        self.assertAllAlmostEqual(res, expected, delta=self.tol)
Example #7
0
 def setUp(self):
     self.dev = DefaultQubit(wires=2, shots=0)