Beispiel #1
0
    def test_numpy_params(self):
        """Test for numpy array as parameter values"""
        qc = RealAmplitudes(num_qubits=2, reps=2)
        qc.measure_all()
        k = 5
        params_array = np.random.rand(k, qc.num_parameters)
        params_list = params_array.tolist()
        params_list_array = list(params_array)
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(circuits=qc)
            target = sampler([0] * k, params_list)

        with self.subTest("ndarrary"):
            with self.assertWarns(DeprecationWarning):
                result = sampler([0] * k, params_array)
            self.assertEqual(len(result.metadata), k)
            for i in range(k):
                self.assertDictEqual(result.quasi_dists[i],
                                     target.quasi_dists[i])

        with self.subTest("list of ndarray"):
            with self.assertWarns(DeprecationWarning):
                result = sampler([0] * k, params_list_array)
            self.assertEqual(len(result.metadata), k)
            for i in range(k):
                self.assertDictEqual(result.quasi_dists[i],
                                     target.quasi_dists[i])
Beispiel #2
0
    def test_run_errors(self):
        """Test for errors"""
        qc1 = QuantumCircuit(1)
        qc1.measure_all()
        qc2 = RealAmplitudes(num_qubits=1, reps=1)
        qc2.measure_all()

        sampler = Sampler()
        with self.assertRaises(QiskitError):
            sampler.run([qc1], [[1e2]]).result()
        with self.assertRaises(QiskitError):
            sampler.run([qc2], [[]]).result()
        with self.assertRaises(QiskitError):
            sampler.run([qc2], [[1e2]]).result()
    def test_errors(self):
        """Test for errors"""
        qc1 = QuantumCircuit(1)
        qc1.measure_all()
        qc2 = RealAmplitudes(num_qubits=1, reps=1)
        qc2.measure_all()

        with Sampler([qc1, qc2], [qc1.parameters, qc2.parameters]) as sampler:
            with self.assertRaises(QiskitError):
                sampler([0], [[1e2]])
            with self.assertRaises(QiskitError):
                sampler([1], [[]])
            with self.assertRaises(QiskitError):
                sampler([1], [[1e2]])
Beispiel #4
0
    def test_fusion_two_qubits(self):
        """Test 2-qubit fusion"""
        shots = 100
        num_qubits = 8
        reps = 3

        circuit = RealAmplitudes(num_qubits=num_qubits,
                                 entanglement='linear',
                                 reps=reps)
        param_binds = {}
        for param in circuit.parameters:
            param_binds[param] = np.random.random()

        circuit = transpile(circuit.bind_parameters(param_binds),
                            backend=self.SIMULATOR,
                            optimization_level=0)
        circuit.measure_all()

        qobj = assemble([circuit],
                        self.SIMULATOR,
                        shots=shots,
                        seed_simulator=1)

        backend_options = self.fusion_options(enabled=True, threshold=1)
        backend_options['fusion_verbose'] = True

        backend_options['fusion_enable.2_qubits'] = False
        result_disabled = self.SIMULATOR.run(qobj, **backend_options).result()
        meta_disabled = self.fusion_metadata(result_disabled)

        backend_options['fusion_enable.2_qubits'] = True
        result_enabled = self.SIMULATOR.run(qobj, **backend_options).result()
        meta_enabled = self.fusion_metadata(result_enabled)

        self.assertTrue(getattr(result_disabled, 'success', 'False'))
        self.assertTrue(getattr(result_enabled, 'success', 'False'))

        self.assertTrue(
            len(meta_enabled['output_ops']) if 'output_ops' in meta_enabled
            else len(circuit.ops) < len(meta_disabled['output_ops'])
            if 'output_ops' in meta_disabled else len(circuit.ops))
Beispiel #5
0
class TestSampler(QiskitTestCase):
    """Test Sampler"""
    def setUp(self):
        super().setUp()
        hadamard = QuantumCircuit(1, 1)
        hadamard.h(0)
        hadamard.measure(0, 0)
        bell = QuantumCircuit(2)
        bell.h(0)
        bell.cx(0, 1)
        bell.measure_all()
        self._circuit = [hadamard, bell]
        self._target = [
            {
                0: 0.5,
                1: 0.5
            },
            {
                0: 0.5,
                3: 0.5,
                1: 0,
                2: 0
            },
        ]
        self._pqc = RealAmplitudes(num_qubits=2, reps=2)
        self._pqc.measure_all()
        self._pqc2 = RealAmplitudes(num_qubits=2, reps=3)
        self._pqc2.measure_all()
        self._pqc_params = [[0.0] * 6, [1.0] * 6]
        self._pqc_target = [{
            0: 1
        }, {
            0: 0.0148,
            1: 0.3449,
            2: 0.0531,
            3: 0.5872
        }]
        self._theta = [
            [0, 1, 1, 2, 3, 5],
            [1, 2, 3, 4, 5, 6],
            [0, 1, 2, 3, 4, 5, 6, 7],
        ]

    def _generate_circuits_target(self, indices):
        if isinstance(indices, list):
            circuits = [self._circuit[j] for j in indices]
            target = [self._target[j] for j in indices]
        else:
            raise ValueError(f"invalid index {indices}")
        return circuits, target

    def _generate_params_target(self, indices):
        if isinstance(indices, int):
            params = self._pqc_params[indices]
            target = self._pqc_target[indices]
        elif isinstance(indices, list):
            params = [self._pqc_params[j] for j in indices]
            target = [self._pqc_target[j] for j in indices]
        else:
            raise ValueError(f"invalid index {indices}")
        return params, target

    def _compare_probs(self, prob, target):
        if not isinstance(prob, list):
            prob = [prob]
        if not isinstance(target, list):
            target = [target]
        self.assertEqual(len(prob), len(target))
        for p, targ in zip(prob, target):
            for key, t_val in targ.items():
                if key in p:
                    self.assertAlmostEqual(p[key], t_val, places=1)
                else:
                    self.assertAlmostEqual(t_val, 0, places=1)

    @combine(indices=[[0], [1], [0, 1]])
    def test_sampler(self, indices):
        """test for sampler"""
        circuits, target = self._generate_circuits_target(indices)
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(circuits=circuits)
            result = sampler(list(range(len(indices))),
                             parameter_values=[[] for _ in indices])
        self._compare_probs(result.quasi_dists, target)

    @combine(indices=[[0], [1], [0, 1]])
    def test_sampler_pqc(self, indices):
        """test for sampler with a parametrized circuit"""
        params, target = self._generate_params_target(indices)
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(circuits=self._pqc)
            result = sampler([0] * len(params), params)
        self._compare_probs(result.quasi_dists, target)

    @combine(indices=[[0, 0], [0, 1], [1, 1]])
    def test_evaluate_two_pqcs(self, indices):
        """test for sampler with two parametrized circuits"""
        circs = [self._pqc, self._pqc]
        params, target = self._generate_params_target(indices)
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(circuits=circs)
            result = sampler(indices, parameter_values=params)
        self._compare_probs(result.quasi_dists, target)

    def test_sampler_example(self):
        """test for Sampler example"""

        bell = QuantumCircuit(2)
        bell.h(0)
        bell.cx(0, 1)
        bell.measure_all()

        # executes a Bell circuit
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(circuits=[bell], parameters=[[]])
            result = sampler(parameter_values=[[]], circuits=[0])
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 1)
        keys, values = zip(*sorted(result.quasi_dists[0].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(values, [0.5, 0, 0, 0.5])
        self.assertEqual(len(result.metadata), 1)

        # executes three Bell circuits
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler([bell] * 3, [[]] * 3)
            result = sampler([0, 1, 2], [[]] * 3)
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 3)
        self.assertEqual(len(result.metadata), 3)
        for dist in result.quasi_dists:
            keys, values = zip(*sorted(dist.items()))
            self.assertTupleEqual(keys, tuple(range(4)))
            np.testing.assert_allclose(values, [0.5, 0, 0, 0.5])

        with self.assertWarns(DeprecationWarning):
            sampler = Sampler([bell])
            result = sampler([bell, bell, bell])
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 3)
        self.assertEqual(len(result.metadata), 3)
        for dist in result.quasi_dists:
            keys, values = zip(*sorted(dist.items()))
            self.assertTupleEqual(keys, tuple(range(4)))
            np.testing.assert_allclose(values, [0.5, 0, 0, 0.5])

        # parametrized circuit
        pqc = RealAmplitudes(num_qubits=2, reps=2)
        pqc.measure_all()
        pqc2 = RealAmplitudes(num_qubits=2, reps=3)
        pqc2.measure_all()

        theta1 = [0, 1, 1, 2, 3, 5]
        theta2 = [1, 2, 3, 4, 5, 6]
        theta3 = [0, 1, 2, 3, 4, 5, 6, 7]

        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(circuits=[pqc, pqc2],
                              parameters=[pqc.parameters, pqc2.parameters])
            result = sampler([0, 0, 1], [theta1, theta2, theta3])
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 3)
        self.assertEqual(len(result.metadata), 3)

        keys, values = zip(*sorted(result.quasi_dists[0].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(
            values,
            [
                0.13092484629757767, 0.3608720796028449, 0.09324865232050054,
                0.414954421779077
            ],
        )

        keys, values = zip(*sorted(result.quasi_dists[1].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(
            values,
            [
                0.06282290651933871, 0.02877144385576703, 0.606654494132085,
                0.3017511554928095
            ],
        )

        keys, values = zip(*sorted(result.quasi_dists[2].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(
            values,
            [
                0.18802639943804164,
                0.6881971261189544,
                0.09326232720582446,
                0.030514147237179882,
            ],
        )

    def test_sampler_param_order(self):
        """test for sampler with different parameter orders"""
        x = Parameter("x")
        y = Parameter("y")

        qc = QuantumCircuit(3, 3)
        qc.rx(x, 0)
        qc.rx(y, 1)
        qc.x(2)
        qc.measure(0, 0)
        qc.measure(1, 1)
        qc.measure(2, 2)

        with self.assertWarns(DeprecationWarning):
            sampler = Sampler([qc, qc], [[x, y], [y, x]])
            result = sampler([0, 1, 0, 1],
                             [[0, 0], [0, 0], [np.pi / 2, 0], [np.pi / 2, 0]])
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 4)

        # qc({x: 0, y: 0})
        keys, values = zip(*sorted(result.quasi_dists[0].items()))
        self.assertTupleEqual(keys, tuple(range(8)))
        np.testing.assert_allclose(values, [0, 0, 0, 0, 1, 0, 0, 0])

        # qc({x: 0, y: 0})
        keys, values = zip(*sorted(result.quasi_dists[1].items()))
        self.assertTupleEqual(keys, tuple(range(8)))
        np.testing.assert_allclose(values, [0, 0, 0, 0, 1, 0, 0, 0])

        # qc({x: pi/2, y: 0})
        keys, values = zip(*sorted(result.quasi_dists[2].items()))
        self.assertTupleEqual(keys, tuple(range(8)))
        np.testing.assert_allclose(values, [0, 0, 0, 0, 0.5, 0.5, 0, 0])

        # qc({x: 0, y: pi/2})
        keys, values = zip(*sorted(result.quasi_dists[3].items()))
        self.assertTupleEqual(keys, tuple(range(8)))
        np.testing.assert_allclose(values, [0, 0, 0, 0, 0.5, 0, 0.5, 0])

    def test_sampler_reverse_meas_order(self):
        """test for sampler with reverse measurement order"""
        x = Parameter("x")
        y = Parameter("y")

        qc = QuantumCircuit(3, 3)
        qc.rx(x, 0)
        qc.rx(y, 1)
        qc.x(2)
        qc.measure(0, 2)
        qc.measure(1, 1)
        qc.measure(2, 0)

        with self.assertWarns(DeprecationWarning):
            sampler = Sampler([qc, qc], [[x, y], [y, x]])
            result = sampler([0, 1, 0, 1],
                             [[0, 0], [0, 0], [np.pi / 2, 0], [np.pi / 2, 0]])
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 4)

        # qc({x: 0, y: 0})
        keys, values = zip(*sorted(result.quasi_dists[0].items()))
        self.assertTupleEqual(keys, tuple(range(8)))
        np.testing.assert_allclose(values, [0, 1, 0, 0, 0, 0, 0, 0])

        # qc({x: 0, y: 0})
        keys, values = zip(*sorted(result.quasi_dists[1].items()))
        self.assertTupleEqual(keys, tuple(range(8)))
        np.testing.assert_allclose(values, [0, 1, 0, 0, 0, 0, 0, 0])

        # qc({x: pi/2, y: 0})
        keys, values = zip(*sorted(result.quasi_dists[2].items()))
        self.assertTupleEqual(keys, tuple(range(8)))
        np.testing.assert_allclose(values, [0, 0.5, 0, 0, 0, 0.5, 0, 0])

        # qc({x: 0, y: pi/2})
        keys, values = zip(*sorted(result.quasi_dists[3].items()))
        self.assertTupleEqual(keys, tuple(range(8)))
        np.testing.assert_allclose(values, [0, 0.5, 0, 0.5, 0, 0, 0, 0])

    def test_1qubit(self):
        """test for 1-qubit cases"""
        qc = QuantumCircuit(1)
        qc.measure_all()
        qc2 = QuantumCircuit(1)
        qc2.x(0)
        qc2.measure_all()

        with self.assertWarns(DeprecationWarning):
            sampler = Sampler([qc, qc2], [qc.parameters, qc2.parameters])
            result = sampler([0, 1], [[]] * 2)
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 2)

        keys, values = zip(*sorted(result.quasi_dists[0].items()))
        self.assertTupleEqual(keys, tuple(range(2)))
        np.testing.assert_allclose(values, [1, 0])

        keys, values = zip(*sorted(result.quasi_dists[1].items()))
        self.assertTupleEqual(keys, tuple(range(2)))
        np.testing.assert_allclose(values, [0, 1])

    def test_2qubit(self):
        """test for 2-qubit cases"""
        qc0 = QuantumCircuit(2)
        qc0.measure_all()
        qc1 = QuantumCircuit(2)
        qc1.x(0)
        qc1.measure_all()
        qc2 = QuantumCircuit(2)
        qc2.x(1)
        qc2.measure_all()
        qc3 = QuantumCircuit(2)
        qc3.x([0, 1])
        qc3.measure_all()

        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(
                [qc0, qc1, qc2, qc3],
                [
                    qc0.parameters, qc1.parameters, qc2.parameters,
                    qc3.parameters
                ],
            )
            result = sampler([0, 1, 2, 3], [[]] * 4)
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 4)

        keys, values = zip(*sorted(result.quasi_dists[0].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(values, [1, 0, 0, 0])

        keys, values = zip(*sorted(result.quasi_dists[1].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(values, [0, 1, 0, 0])

        keys, values = zip(*sorted(result.quasi_dists[2].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(values, [0, 0, 1, 0])

        keys, values = zip(*sorted(result.quasi_dists[3].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(values, [0, 0, 0, 1])

    def test_errors(self):
        """Test for errors"""
        qc1 = QuantumCircuit(1)
        qc1.measure_all()
        qc2 = RealAmplitudes(num_qubits=1, reps=1)
        qc2.measure_all()

        with self.assertWarns(DeprecationWarning):
            sampler = Sampler([qc1, qc2], [qc1.parameters, qc2.parameters])
        with self.assertRaises(QiskitError), self.assertWarns(
                DeprecationWarning):
            sampler([0], [[1e2]])
        with self.assertRaises(QiskitError), self.assertWarns(
                DeprecationWarning):
            sampler([1], [[]])
        with self.assertRaises(QiskitError), self.assertWarns(
                DeprecationWarning):
            sampler([1], [[1e2]])

    def test_empty_parameter(self):
        """Test for empty parameter"""
        n = 5
        qc = QuantumCircuit(n, n - 1)
        qc.measure(range(n - 1), range(n - 1))
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(circuits=[qc] * 10)
        with self.subTest("one circuit"):
            with self.assertWarns(DeprecationWarning):
                result = sampler([0], shots=1000)
            self.assertEqual(len(result.quasi_dists), 1)
            for q_d in result.quasi_dists:
                quasi_dist = {k: v for k, v in q_d.items() if v != 0.0}
                self.assertDictEqual(quasi_dist, {0: 1.0})
            self.assertEqual(len(result.metadata), 1)

        with self.subTest("two circuits"):
            with self.assertWarns(DeprecationWarning):
                result = sampler([2, 4], shots=1000)
            self.assertEqual(len(result.quasi_dists), 2)
            for q_d in result.quasi_dists:
                quasi_dist = {k: v for k, v in q_d.items() if v != 0.0}
                self.assertDictEqual(quasi_dist, {0: 1.0})
            self.assertEqual(len(result.metadata), 2)

    def test_numpy_params(self):
        """Test for numpy array as parameter values"""
        qc = RealAmplitudes(num_qubits=2, reps=2)
        qc.measure_all()
        k = 5
        params_array = np.random.rand(k, qc.num_parameters)
        params_list = params_array.tolist()
        params_list_array = list(params_array)
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(circuits=qc)
            target = sampler([0] * k, params_list)

        with self.subTest("ndarrary"):
            with self.assertWarns(DeprecationWarning):
                result = sampler([0] * k, params_array)
            self.assertEqual(len(result.metadata), k)
            for i in range(k):
                self.assertDictEqual(result.quasi_dists[i],
                                     target.quasi_dists[i])

        with self.subTest("list of ndarray"):
            with self.assertWarns(DeprecationWarning):
                result = sampler([0] * k, params_list_array)
            self.assertEqual(len(result.metadata), k)
            for i in range(k):
                self.assertDictEqual(result.quasi_dists[i],
                                     target.quasi_dists[i])

    def test_passing_objects(self):
        """Test passing objects for Sampler."""

        params, target = self._generate_params_target([0])

        with self.subTest("Valid test"):
            with self.assertWarns(DeprecationWarning):
                sampler = Sampler(circuits=self._pqc)
                result = sampler(circuits=[self._pqc], parameter_values=params)
            self._compare_probs(result.quasi_dists, target)

        with self.subTest("Invalid circuit test"):
            circuit = QuantumCircuit(2)
            with self.assertWarns(DeprecationWarning):
                sampler = Sampler(circuits=self._pqc)
            with self.assertRaises(QiskitError), self.assertWarns(
                    DeprecationWarning):
                result = sampler(circuits=[circuit], parameter_values=params)

    @combine(indices=[[0], [1], [0, 1]])
    def test_deprecated_circuit_indices(self, indices):
        """Test for deprecated arguments"""
        circuits, target = self._generate_circuits_target(indices)
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(circuits=circuits)
            result = sampler(
                circuit_indices=list(range(len(indices))),
                parameter_values=[[] for _ in indices],
            )
        self._compare_probs(result.quasi_dists, target)

    def test_with_shots_option(self):
        """test with shots option."""
        params, target = self._generate_params_target([1])
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(circuits=self._pqc)
            result = sampler(circuits=[0],
                             parameter_values=params,
                             shots=1024,
                             seed=15)
        self._compare_probs(result.quasi_dists, target)

    def test_with_shots_option_none(self):
        """test with shots=None option. Seed is ignored then."""
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler([self._pqc])
            result_42 = sampler([0],
                                parameter_values=[[0, 1, 1, 2, 3, 5]],
                                shots=None,
                                seed=42)
            result_15 = sampler([0],
                                parameter_values=[[0, 1, 1, 2, 3, 5]],
                                shots=None,
                                seed=15)
        self.assertDictAlmostEqual(result_42.quasi_dists,
                                   result_15.quasi_dists)

    def test_sampler_run(self):
        """Test Sampler.run()."""
        bell = self._circuit[1]
        sampler = Sampler()
        job = sampler.run(circuits=[bell])
        self.assertIsInstance(job, JobV1)
        result = job.result()
        self.assertIsInstance(result, SamplerResult)
        # print([q.binary_probabilities() for q in result.quasi_dists])
        self._compare_probs(result.quasi_dists, self._target[1])

    def test_sample_run_multiple_circuits(self):
        """Test Sampler.run() with multiple circuits."""
        # executes three Bell circuits
        # Argument `parameters` is optional.
        bell = self._circuit[1]
        sampler = Sampler()
        result = sampler.run([bell, bell, bell]).result()
        # print([q.binary_probabilities() for q in result.quasi_dists])
        self._compare_probs(result.quasi_dists[0], self._target[1])
        self._compare_probs(result.quasi_dists[1], self._target[1])
        self._compare_probs(result.quasi_dists[2], self._target[1])

    def test_sampler_run_with_parameterized_circuits(self):
        """Test Sampler.run() with parameterized circuits."""
        # parameterized circuit

        pqc = self._pqc
        pqc2 = self._pqc2
        theta1, theta2, theta3 = self._theta

        sampler = Sampler()
        result = sampler.run([pqc, pqc, pqc2],
                             [theta1, theta2, theta3]).result()

        # result of pqc(theta1)
        prob1 = {
            "00": 0.1309248462975777,
            "01": 0.3608720796028448,
            "10": 0.09324865232050054,
            "11": 0.41495442177907715,
        }
        self.assertDictAlmostEqual(
            result.quasi_dists[0].binary_probabilities(), prob1)

        # result of pqc(theta2)
        prob2 = {
            "00": 0.06282290651933871,
            "01": 0.02877144385576705,
            "10": 0.606654494132085,
            "11": 0.3017511554928094,
        }
        self.assertDictAlmostEqual(
            result.quasi_dists[1].binary_probabilities(), prob2)

        # result of pqc2(theta3)
        prob3 = {
            "00": 0.1880263994380416,
            "01": 0.6881971261189544,
            "10": 0.09326232720582443,
            "11": 0.030514147237179892,
        }
        self.assertDictAlmostEqual(
            result.quasi_dists[2].binary_probabilities(), prob3)

    def test_run_1qubit(self):
        """test for 1-qubit cases"""
        qc = QuantumCircuit(1)
        qc.measure_all()
        qc2 = QuantumCircuit(1)
        qc2.x(0)
        qc2.measure_all()

        sampler = Sampler()
        result = sampler.run([qc, qc2]).result()
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 2)

        keys, values = zip(*sorted(result.quasi_dists[0].items()))
        self.assertTupleEqual(keys, tuple(range(2)))
        np.testing.assert_allclose(values, [1, 0])

        keys, values = zip(*sorted(result.quasi_dists[1].items()))
        self.assertTupleEqual(keys, tuple(range(2)))
        np.testing.assert_allclose(values, [0, 1])

    def test_run_2qubit(self):
        """test for 2-qubit cases"""
        qc0 = QuantumCircuit(2)
        qc0.measure_all()
        qc1 = QuantumCircuit(2)
        qc1.x(0)
        qc1.measure_all()
        qc2 = QuantumCircuit(2)
        qc2.x(1)
        qc2.measure_all()
        qc3 = QuantumCircuit(2)
        qc3.x([0, 1])
        qc3.measure_all()

        sampler = Sampler()
        result = sampler.run([qc0, qc1, qc2, qc3]).result()
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 4)

        keys, values = zip(*sorted(result.quasi_dists[0].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(values, [1, 0, 0, 0])

        keys, values = zip(*sorted(result.quasi_dists[1].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(values, [0, 1, 0, 0])

        keys, values = zip(*sorted(result.quasi_dists[2].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(values, [0, 0, 1, 0])

        keys, values = zip(*sorted(result.quasi_dists[3].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(values, [0, 0, 0, 1])

    def test_run_errors(self):
        """Test for errors"""
        qc1 = QuantumCircuit(1)
        qc1.measure_all()
        qc2 = RealAmplitudes(num_qubits=1, reps=1)
        qc2.measure_all()

        sampler = Sampler()
        with self.assertRaises(QiskitError):
            sampler.run([qc1], [[1e2]]).result()
        with self.assertRaises(QiskitError):
            sampler.run([qc2], [[]]).result()
        with self.assertRaises(QiskitError):
            sampler.run([qc2], [[1e2]]).result()

    def test_run_empty_parameter(self):
        """Test for empty parameter"""
        n = 5
        qc = QuantumCircuit(n, n - 1)
        qc.measure(range(n - 1), range(n - 1))
        sampler = Sampler()
        with self.subTest("one circuit"):
            result = sampler.run([qc], shots=1000).result()
            self.assertEqual(len(result.quasi_dists), 1)
            for q_d in result.quasi_dists:
                quasi_dist = {k: v for k, v in q_d.items() if v != 0.0}
                self.assertDictEqual(quasi_dist, {0: 1.0})
            self.assertEqual(len(result.metadata), 1)

        with self.subTest("two circuits"):
            result = sampler.run([qc, qc], shots=1000).result()
            self.assertEqual(len(result.quasi_dists), 2)
            for q_d in result.quasi_dists:
                quasi_dist = {k: v for k, v in q_d.items() if v != 0.0}
                self.assertDictEqual(quasi_dist, {0: 1.0})
            self.assertEqual(len(result.metadata), 2)

    def test_run_numpy_params(self):
        """Test for numpy array as parameter values"""
        qc = RealAmplitudes(num_qubits=2, reps=2)
        qc.measure_all()
        k = 5
        params_array = np.random.rand(k, qc.num_parameters)
        params_list = params_array.tolist()
        params_list_array = list(params_array)
        sampler = Sampler()
        target = sampler.run([qc] * k, params_list).result()

        with self.subTest("ndarrary"):
            result = sampler.run([qc] * k, params_array).result()
            self.assertEqual(len(result.metadata), k)
            for i in range(k):
                self.assertDictEqual(result.quasi_dists[i],
                                     target.quasi_dists[i])

        with self.subTest("list of ndarray"):
            result = sampler.run([qc] * k, params_list_array).result()
            self.assertEqual(len(result.metadata), k)
            for i in range(k):
                self.assertDictEqual(result.quasi_dists[i],
                                     target.quasi_dists[i])

    def test_run_with_shots_option(self):
        """test with shots option."""
        params, target = self._generate_params_target([1])
        sampler = Sampler()
        result = sampler.run(circuits=[self._pqc],
                             parameter_values=params,
                             shots=1024,
                             seed=15).result()
        self._compare_probs(result.quasi_dists, target)

    def test_run_with_shots_option_none(self):
        """test with shots=None option. Seed is ignored then."""
        sampler = Sampler()
        result_42 = sampler.run([self._pqc],
                                parameter_values=[[0, 1, 1, 2, 3, 5]],
                                shots=None,
                                seed=42).result()
        result_15 = sampler.run([self._pqc],
                                parameter_values=[[0, 1, 1, 2, 3, 5]],
                                shots=None,
                                seed=15).result()
        self.assertDictAlmostEqual(result_42.quasi_dists,
                                   result_15.quasi_dists)

    def test_primitive_job_status_done(self):
        """test primitive job's status"""
        bell = self._circuit[1]
        sampler = Sampler()
        job = sampler.run(circuits=[bell])
        self.assertEqual(job.status(), JobStatus.DONE)
Beispiel #6
0
    def test_sampler_example(self):
        """test for Sampler example"""

        bell = QuantumCircuit(2)
        bell.h(0)
        bell.cx(0, 1)
        bell.measure_all()

        # executes a Bell circuit
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(circuits=[bell], parameters=[[]])
            result = sampler(parameter_values=[[]], circuits=[0])
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 1)
        keys, values = zip(*sorted(result.quasi_dists[0].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(values, [0.5, 0, 0, 0.5])
        self.assertEqual(len(result.metadata), 1)

        # executes three Bell circuits
        with self.assertWarns(DeprecationWarning):
            sampler = Sampler([bell] * 3, [[]] * 3)
            result = sampler([0, 1, 2], [[]] * 3)
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 3)
        self.assertEqual(len(result.metadata), 3)
        for dist in result.quasi_dists:
            keys, values = zip(*sorted(dist.items()))
            self.assertTupleEqual(keys, tuple(range(4)))
            np.testing.assert_allclose(values, [0.5, 0, 0, 0.5])

        with self.assertWarns(DeprecationWarning):
            sampler = Sampler([bell])
            result = sampler([bell, bell, bell])
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 3)
        self.assertEqual(len(result.metadata), 3)
        for dist in result.quasi_dists:
            keys, values = zip(*sorted(dist.items()))
            self.assertTupleEqual(keys, tuple(range(4)))
            np.testing.assert_allclose(values, [0.5, 0, 0, 0.5])

        # parametrized circuit
        pqc = RealAmplitudes(num_qubits=2, reps=2)
        pqc.measure_all()
        pqc2 = RealAmplitudes(num_qubits=2, reps=3)
        pqc2.measure_all()

        theta1 = [0, 1, 1, 2, 3, 5]
        theta2 = [1, 2, 3, 4, 5, 6]
        theta3 = [0, 1, 2, 3, 4, 5, 6, 7]

        with self.assertWarns(DeprecationWarning):
            sampler = Sampler(circuits=[pqc, pqc2],
                              parameters=[pqc.parameters, pqc2.parameters])
            result = sampler([0, 0, 1], [theta1, theta2, theta3])
        self.assertIsInstance(result, SamplerResult)
        self.assertEqual(len(result.quasi_dists), 3)
        self.assertEqual(len(result.metadata), 3)

        keys, values = zip(*sorted(result.quasi_dists[0].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(
            values,
            [
                0.13092484629757767, 0.3608720796028449, 0.09324865232050054,
                0.414954421779077
            ],
        )

        keys, values = zip(*sorted(result.quasi_dists[1].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(
            values,
            [
                0.06282290651933871, 0.02877144385576703, 0.606654494132085,
                0.3017511554928095
            ],
        )

        keys, values = zip(*sorted(result.quasi_dists[2].items()))
        self.assertTupleEqual(keys, tuple(range(4)))
        np.testing.assert_allclose(
            values,
            [
                0.18802639943804164,
                0.6881971261189544,
                0.09326232720582446,
                0.030514147237179882,
            ],
        )
class TestSampler(QiskitTestCase):
    """Test Sampler"""

    def setUp(self):
        super().setUp()
        hadamard = QuantumCircuit(1, 1)
        hadamard.h(0)
        hadamard.measure(0, 0)
        bell = QuantumCircuit(2)
        bell.h(0)
        bell.cx(0, 1)
        bell.measure_all()
        self._circuit = [hadamard, bell]
        self._target = [
            {0: 0.5, 1: 0.5},
            {0: 0.5, 3: 0.5, 1: 0, 2: 0},
        ]
        self._pqc = RealAmplitudes(num_qubits=2, reps=2)
        self._pqc.measure_all()
        self._pqc_params = [[0.0] * 6, [1.0] * 6]
        self._pqc_target = [{0: 1}, {0: 0.0148, 1: 0.3449, 2: 0.0531, 3: 0.5872}]

    def _generate_circuits_target(self, indices):
        if isinstance(indices, list):
            circuits = [self._circuit[j] for j in indices]
            target = [self._target[j] for j in indices]
        else:
            raise ValueError(f"invalid index {indices}")
        return circuits, target

    def _generate_params_target(self, indices):
        if isinstance(indices, int):
            params = self._pqc_params[indices]
            target = self._pqc_target[indices]
        elif isinstance(indices, list):
            params = [self._pqc_params[j] for j in indices]
            target = [self._pqc_target[j] for j in indices]
        else:
            raise ValueError(f"invalid index {indices}")
        return params, target

    def _compare_probs(self, prob, target):
        if not isinstance(target, list):
            target = [target]
        self.assertEqual(len(prob), len(target))
        for p, targ in zip(prob, target):
            for key, t_val in targ.items():
                if key in p:
                    self.assertAlmostEqual(p[key], t_val, places=1)
                else:
                    self.assertAlmostEqual(t_val, 0, places=1)

    @combine(indices=[[0], [1], [0, 1]])
    def test_sampler(self, indices):
        """test for sampler"""
        circuits, target = self._generate_circuits_target(indices)
        with Sampler(circuits=circuits) as sampler:
            result = sampler(parameter_values=[[] for _ in indices])
            self._compare_probs(result.quasi_dists, target)

    @combine(indices=[[0], [1], [0, 1]])
    def test_sampler_pqc(self, indices):
        """test for sampler with a parametrized circuit"""
        params, target = self._generate_params_target(indices)
        with Sampler(circuits=self._pqc) as sampler:
            result = sampler([0] * len(params), params)
            self._compare_probs(result.quasi_dists, target)

    @combine(indices=[[0, 0], [0, 1], [1, 1]])
    def test_evaluate_two_pqcs(self, indices):
        """test for sampler with two parametrized circuits"""
        circs = [self._pqc, self._pqc]
        params, target = self._generate_params_target(indices)
        with Sampler(circuits=circs) as sampler:
            result = sampler(parameter_values=params)
            self._compare_probs(result.quasi_dists, target)

    def test_sampler_example(self):
        """test for Sampler example"""

        bell = QuantumCircuit(2)
        bell.h(0)
        bell.cx(0, 1)
        bell.measure_all()

        # executes a Bell circuit
        with Sampler(circuits=[bell], parameters=[[]]) as sampler:
            result = sampler(parameter_values=[[]], circuits=[0])
            self.assertIsInstance(result, SamplerResult)
            self.assertEqual(len(result.quasi_dists), 1)
            keys, values = zip(*sorted(result.quasi_dists[0].items()))
            self.assertTupleEqual(keys, tuple(range(4)))
            np.testing.assert_allclose(values, [0.5, 0, 0, 0.5])
            self.assertEqual(len(result.metadata), 1)

        # executes three Bell circuits
        with Sampler([bell] * 3, [[]] * 3) as sampler:
            result = sampler([0, 1, 2], [[]] * 3)
            self.assertIsInstance(result, SamplerResult)
            self.assertEqual(len(result.quasi_dists), 3)
            self.assertEqual(len(result.metadata), 3)
            for dist in result.quasi_dists:
                keys, values = zip(*sorted(dist.items()))
                self.assertTupleEqual(keys, tuple(range(4)))
                np.testing.assert_allclose(values, [0.5, 0, 0, 0.5])

        # parametrized circuit
        pqc = RealAmplitudes(num_qubits=2, reps=2)
        pqc.measure_all()
        pqc2 = RealAmplitudes(num_qubits=2, reps=3)
        pqc2.measure_all()

        theta1 = [0, 1, 1, 2, 3, 5]
        theta2 = [1, 2, 3, 4, 5, 6]
        theta3 = [0, 1, 2, 3, 4, 5, 6, 7]

        with Sampler(circuits=[pqc, pqc2], parameters=[pqc.parameters, pqc2.parameters]) as sampler:
            result = sampler([0, 0, 1], [theta1, theta2, theta3])
            self.assertIsInstance(result, SamplerResult)
            self.assertEqual(len(result.quasi_dists), 3)
            self.assertEqual(len(result.metadata), 3)

            keys, values = zip(*sorted(result.quasi_dists[0].items()))
            self.assertTupleEqual(keys, tuple(range(4)))
            np.testing.assert_allclose(
                values,
                [0.13092484629757767, 0.3608720796028449, 0.09324865232050054, 0.414954421779077],
            )

            keys, values = zip(*sorted(result.quasi_dists[1].items()))
            self.assertTupleEqual(keys, tuple(range(4)))
            np.testing.assert_allclose(
                values,
                [0.06282290651933871, 0.02877144385576703, 0.606654494132085, 0.3017511554928095],
            )

            keys, values = zip(*sorted(result.quasi_dists[2].items()))
            self.assertTupleEqual(keys, tuple(range(4)))
            np.testing.assert_allclose(
                values,
                [
                    0.18802639943804164,
                    0.6881971261189544,
                    0.09326232720582446,
                    0.030514147237179882,
                ],
            )

    def test_sampler_param_order(self):
        """test for sampler with different parameter orders"""
        x = Parameter("x")
        y = Parameter("y")

        qc = QuantumCircuit(3, 3)
        qc.rx(x, 0)
        qc.rx(y, 1)
        qc.x(2)
        qc.measure(0, 0)
        qc.measure(1, 1)
        qc.measure(2, 2)

        with Sampler([qc, qc], [[x, y], [y, x]]) as sampler:
            result = sampler([0, 1, 0, 1], [[0, 0], [0, 0], [np.pi / 2, 0], [np.pi / 2, 0]])
            self.assertIsInstance(result, SamplerResult)
            self.assertEqual(len(result.quasi_dists), 4)

            # qc({x: 0, y: 0})
            keys, values = zip(*sorted(result.quasi_dists[0].items()))
            self.assertTupleEqual(keys, tuple(range(8)))
            np.testing.assert_allclose(values, [0, 0, 0, 0, 1, 0, 0, 0])

            # qc({x: 0, y: 0})
            keys, values = zip(*sorted(result.quasi_dists[1].items()))
            self.assertTupleEqual(keys, tuple(range(8)))
            np.testing.assert_allclose(values, [0, 0, 0, 0, 1, 0, 0, 0])

            # qc({x: pi/2, y: 0})
            keys, values = zip(*sorted(result.quasi_dists[2].items()))
            self.assertTupleEqual(keys, tuple(range(8)))
            np.testing.assert_allclose(values, [0, 0, 0, 0, 0.5, 0.5, 0, 0])

            # qc({x: 0, y: pi/2})
            keys, values = zip(*sorted(result.quasi_dists[3].items()))
            self.assertTupleEqual(keys, tuple(range(8)))
            np.testing.assert_allclose(values, [0, 0, 0, 0, 0.5, 0, 0.5, 0])

    def test_sampler_reverse_meas_order(self):
        """test for sampler with reverse measurement order"""
        x = Parameter("x")
        y = Parameter("y")

        qc = QuantumCircuit(3, 3)
        qc.rx(x, 0)
        qc.rx(y, 1)
        qc.x(2)
        qc.measure(0, 2)
        qc.measure(1, 1)
        qc.measure(2, 0)

        with Sampler([qc, qc], [[x, y], [y, x]]) as sampler:
            result = sampler([0, 1, 0, 1], [[0, 0], [0, 0], [np.pi / 2, 0], [np.pi / 2, 0]])
            self.assertIsInstance(result, SamplerResult)
            self.assertEqual(len(result.quasi_dists), 4)

            # qc({x: 0, y: 0})
            keys, values = zip(*sorted(result.quasi_dists[0].items()))
            self.assertTupleEqual(keys, tuple(range(8)))
            np.testing.assert_allclose(values, [0, 1, 0, 0, 0, 0, 0, 0])

            # qc({x: 0, y: 0})
            keys, values = zip(*sorted(result.quasi_dists[1].items()))
            self.assertTupleEqual(keys, tuple(range(8)))
            np.testing.assert_allclose(values, [0, 1, 0, 0, 0, 0, 0, 0])

            # qc({x: pi/2, y: 0})
            keys, values = zip(*sorted(result.quasi_dists[2].items()))
            self.assertTupleEqual(keys, tuple(range(8)))
            np.testing.assert_allclose(values, [0, 0.5, 0, 0, 0, 0.5, 0, 0])

            # qc({x: 0, y: pi/2})
            keys, values = zip(*sorted(result.quasi_dists[3].items()))
            self.assertTupleEqual(keys, tuple(range(8)))
            np.testing.assert_allclose(values, [0, 0.5, 0, 0.5, 0, 0, 0, 0])

    def test_1qubit(self):
        """test for 1-qubit cases"""
        qc = QuantumCircuit(1)
        qc.measure_all()
        qc2 = QuantumCircuit(1)
        qc2.x(0)
        qc2.measure_all()

        with Sampler([qc, qc2], [qc.parameters, qc2.parameters]) as sampler:
            result = sampler([0, 1], [[]] * 2)
            self.assertIsInstance(result, SamplerResult)
            self.assertEqual(len(result.quasi_dists), 2)

            keys, values = zip(*sorted(result.quasi_dists[0].items()))
            self.assertTupleEqual(keys, tuple(range(2)))
            np.testing.assert_allclose(values, [1, 0])

            keys, values = zip(*sorted(result.quasi_dists[1].items()))
            self.assertTupleEqual(keys, tuple(range(2)))
            np.testing.assert_allclose(values, [0, 1])

    def test_2qubit(self):
        """test for 2-qubit cases"""
        qc0 = QuantumCircuit(2)
        qc0.measure_all()
        qc1 = QuantumCircuit(2)
        qc1.x(0)
        qc1.measure_all()
        qc2 = QuantumCircuit(2)
        qc2.x(1)
        qc2.measure_all()
        qc3 = QuantumCircuit(2)
        qc3.x([0, 1])
        qc3.measure_all()

        with Sampler(
            [qc0, qc1, qc2, qc3], [qc0.parameters, qc1.parameters, qc2.parameters, qc3.parameters]
        ) as sampler:
            result = sampler([0, 1, 2, 3], [[]] * 4)
            self.assertIsInstance(result, SamplerResult)
            self.assertEqual(len(result.quasi_dists), 4)

            keys, values = zip(*sorted(result.quasi_dists[0].items()))
            self.assertTupleEqual(keys, tuple(range(4)))
            np.testing.assert_allclose(values, [1, 0, 0, 0])

            keys, values = zip(*sorted(result.quasi_dists[1].items()))
            self.assertTupleEqual(keys, tuple(range(4)))
            np.testing.assert_allclose(values, [0, 1, 0, 0])

            keys, values = zip(*sorted(result.quasi_dists[2].items()))
            self.assertTupleEqual(keys, tuple(range(4)))
            np.testing.assert_allclose(values, [0, 0, 1, 0])

            keys, values = zip(*sorted(result.quasi_dists[3].items()))
            self.assertTupleEqual(keys, tuple(range(4)))
            np.testing.assert_allclose(values, [0, 0, 0, 1])

    def test_errors(self):
        """Test for errors"""
        qc1 = QuantumCircuit(1)
        qc1.measure_all()
        qc2 = RealAmplitudes(num_qubits=1, reps=1)
        qc2.measure_all()

        with Sampler([qc1, qc2], [qc1.parameters, qc2.parameters]) as sampler:
            with self.assertRaises(QiskitError):
                sampler([0], [[1e2]])
            with self.assertRaises(QiskitError):
                sampler([1], [[]])
            with self.assertRaises(QiskitError):
                sampler([1], [[1e2]])

    def test_empty_parameter(self):
        """Test for empty parameter"""
        n = 5
        qc = QuantumCircuit(n, n - 1)
        qc.measure(range(n - 1), range(n - 1))
        with Sampler(circuits=[qc] * 10) as sampler:
            with self.subTest("one circuit"):
                result = sampler([0], shots=1000)
                self.assertEqual(len(result.quasi_dists), 1)
                for q_d in result.quasi_dists:
                    quasi_dist = {k: v for k, v in q_d.items() if v != 0.0}
                    self.assertDictEqual(quasi_dist, {0: 1.0})
                self.assertEqual(len(result.metadata), 1)

            with self.subTest("two circuits"):
                result = sampler([2, 4], shots=1000)
                self.assertEqual(len(result.quasi_dists), 2)
                for q_d in result.quasi_dists:
                    quasi_dist = {k: v for k, v in q_d.items() if v != 0.0}
                    self.assertDictEqual(quasi_dist, {0: 1.0})
                self.assertEqual(len(result.metadata), 2)

    def test_numpy_params(self):
        """Test for numpy array as parameter values"""
        qc = RealAmplitudes(num_qubits=2, reps=2)
        qc.measure_all()
        k = 5
        params_array = np.random.rand(k, qc.num_parameters)
        params_list = params_array.tolist()
        params_list_array = list(params_array)
        with Sampler(circuits=qc) as sampler:
            target = sampler([0] * k, params_list)

            with self.subTest("ndarrary"):
                result = sampler([0] * k, params_array)
                self.assertEqual(len(result.metadata), k)
                for i in range(k):
                    self.assertDictEqual(result.quasi_dists[i], target.quasi_dists[i])

            with self.subTest("list of ndarray"):
                result = sampler([0] * k, params_list_array)
                self.assertEqual(len(result.metadata), k)
                for i in range(k):
                    self.assertDictEqual(result.quasi_dists[i], target.quasi_dists[i])