def test_qasm_auto_enable_shot_parallelization_with_single_precision(self):
        """test auto-enabling max_parallel_shots with single-precision"""
        # Test circuit
        max_threads = 2
        shots = 2
        circuit = quantum_volume_circuit(17, 1, measure=True, seed=0)

        backend_opts = self.BACKEND_OPTS.copy()
        backend_opts['max_parallel_threads'] = max_threads
        backend_opts['max_parallel_shots'] = shots
        backend_opts['noise_model'] = self.dummy_noise_model()
        backend_opts['max_memory_mb'] = 2
        backend_opts['precision'] = "single"

        result = execute(circuit,
                         self.SIMULATOR,
                         shots=shots,
                         backend_options=backend_opts).result()
        if result.metadata['omp_enabled']:
            self.assertEqual(result.metadata['parallel_experiments'],
                             1,
                             msg="parallel_experiments should be 1")
            self.assertEqual(
                result.to_dict()['results'][0]['metadata']['parallel_shots'],
                shots,
                msg="parallel_shots must be " + str(shots))
            self.assertEqual(result.to_dict()['results'][0]['metadata']
                             ['parallel_state_update'],
                             1,
                             msg="parallel_state_update should be 1")
    def _test_qasm_explicit_parallelization(self):
        """test disabling parallel shots because max_parallel_shots is 1"""
        # Test circuit
        shots = multiprocessing.cpu_count()
        circuit = quantum_volume_circuit(16, 1, measure=True, seed=0)

        backend_opts = self.BACKEND_OPTS.copy()
        backend_opts['max_parallel_shots'] = 1
        backend_opts['max_parallel_experiments'] = 1
        backend_opts['noise_model'] = self.dummy_noise_model()
        backend_opts['_parallel_experiments'] = 2
        backend_opts['_parallel_shots'] = 3
        backend_opts['_parallel_state_update'] = 4

        result = execute(circuit,
                         self.SIMULATOR,
                         shots=shots,
                         backend_options=backend_opts).result()
        if result.metadata['omp_enabled']:
            self.assertEqual(result.metadata['parallel_experiments'],
                             2,
                             msg="parallel_experiments should be 2")
            self.assertEqual(
                result.to_dict()['results'][0]['metadata']['parallel_shots'],
                3,
                msg="parallel_shots must be 3")
            self.assertEqual(result.to_dict()['results'][0]['metadata']
                             ['parallel_state_update'],
                             4,
                             msg="parallel_state_update should be 4")
    def test_qasm_auto_disable_shot_parallelization_with_sampling(self):
        """test auto-disabling max_parallel_shots because sampling is enabled"""
        # Test circuit
        shots = multiprocessing.cpu_count()
        circuit = quantum_volume_circuit(4, 1, measure=True, seed=0)

        backend_opts = self.BACKEND_OPTS.copy()
        backend_opts['max_parallel_shots'] = shots

        result = execute(circuit,
                         self.SIMULATOR,
                         shots=shots,
                         backend_options=backend_opts).result()
        if result.metadata['omp_enabled']:
            self.assertEqual(result.metadata['parallel_experiments'],
                             1,
                             msg="parallel_experiments should be 1")
            self.assertEqual(
                result.to_dict()['results'][0]['metadata']['parallel_shots'],
                1,
                msg="parallel_shots must be 1")
            self.assertEqual(result.to_dict()['results'][0]['metadata']
                             ['parallel_state_update'],
                             multiprocessing.cpu_count(),
                             msg="parallel_state_update should be " +
                             str(multiprocessing.cpu_count()))
    def test_qasm_auto_short_shot_parallelization(self):
        """test auto-disabling max_parallel_shots because a number of shots is few"""
        # Test circuit
        max_threads = 4
        shots = 2
        circuit = quantum_volume_circuit(4, 1, measure=True, seed=0)

        backend_opts = self.BACKEND_OPTS.copy()
        backend_opts['max_parallel_threads'] = max_threads
        backend_opts['max_parallel_shots'] = max_threads
        backend_opts['noise_model'] = self.dummy_noise_model()

        result = execute(circuit,
                         self.SIMULATOR,
                         shots=shots,
                         backend_options=backend_opts).result()
        if result.metadata['omp_enabled']:
            self.assertEqual(result.metadata['parallel_experiments'],
                             1,
                             msg="parallel_experiments should be 1")
            self.assertEqual(
                result.to_dict()['results'][0]['metadata']['parallel_shots'],
                shots,
                msg="parallel_shots must be " + str(shots))
            self.assertEqual(result.to_dict()['results'][0]['metadata']
                             ['parallel_state_update'],
                             max_threads / shots,
                             msg="parallel_state_update should be " +
                             str(max_threads / shots))
    def test_qasm_auto_disable_parallel_experiments_with_memory_shortage(self):
        """test auto-disabling max_parallel_experiments because memory is short"""
        # Test circuit
        shots = 100
        experiments = multiprocessing.cpu_count()
        circuits = []
        for _ in range(experiments):
            circuits.append(quantum_volume_circuit(16, 1, measure=True,
                                                   seed=0))  # 2 MB for each

        backend_opts = self.BACKEND_OPTS.copy()
        backend_opts['max_parallel_experiments'] = experiments
        backend_opts['max_memory_mb'] = 1

        result = execute(circuits,
                         self.SIMULATOR,
                         shots=shots,
                         backend_options=backend_opts).result()
        if result.metadata['omp_enabled']:
            self.assertEqual(result.metadata['parallel_experiments'],
                             1,
                             msg="parallel_experiments should be 1")
            self.assertEqual(
                result.to_dict()['results'][0]['metadata']['parallel_shots'],
                1,
                msg="parallel_shots must be 1")
            self.assertEqual(result.to_dict()['results'][0]['metadata']
                             ['parallel_state_update'],
                             multiprocessing.cpu_count(),
                             msg="parallel_state_update should be " +
                             str(multiprocessing.cpu_count()))
    def test_qasm_auto_short_parallel_experiments(self):
        """test auto-disabling max_parallel_experiments because a number of circuits is few"""
        # Test circuit
        shots = 1
        experiments = multiprocessing.cpu_count()
        if experiments == 1:
            return
        circuits = []
        for _ in range(experiments - 1):
            circuits.append(quantum_volume_circuit(4, 1, measure=True,
                                                   seed=0))  # 2 MB for each

        backend_opts = self.BACKEND_OPTS.copy()
        backend_opts['max_parallel_experiments'] = experiments
        backend_opts['max_memory_mb'] = 1024

        result = execute(circuits,
                         self.SIMULATOR,
                         shots=shots,
                         backend_options=backend_opts).result()
        if result.metadata['omp_enabled']:
            self.assertEqual(result.metadata['parallel_experiments'],
                             multiprocessing.cpu_count() - 1,
                             msg="parallel_experiments should be {}".format(
                                 multiprocessing.cpu_count() - 1))
            self.assertEqual(
                result.to_dict()['results'][0]['metadata']['parallel_shots'],
                1,
                msg="parallel_shots must be 1")
 def test_qasm_auto_disable_shot_parallelization_with_memory_shortage(self):
     """test auto-disabling max_parallel_shots because memory is short"""
     # Test circuit
     shots = multiprocessing.cpu_count()
     circuit = quantum_volume_circuit(16, 1, measure=True, seed=0)
     qobj = compile(circuit, self.SIMULATOR, shots=shots)
     backend_opts = {
         'max_parallel_shots': shots,
         'noise_model': self.dummy_noise_model(),
         'max_memory_mb': 1
     }
     result = self.SIMULATOR.run(qobj,
                                 backend_options=backend_opts).result()
     if result.metadata['omp_enabled']:
         self.assertEqual(result.metadata['parallel_experiments'],
                          1,
                          msg="parallel_experiments should be 1")
         self.assertEqual(
             result.to_dict()['results'][0]['metadata']['parallel_shots'],
             1,
             msg="parallel_shots must be 1")
         self.assertEqual(result.to_dict()['results'][0]['metadata']
                          ['parallel_state_update'],
                          multiprocessing.cpu_count(),
                          msg="parallel_state_update should be " +
                          str(multiprocessing.cpu_count()))
Beispiel #8
0
    def test_max_memory_settings(self):
        """test max memory configuration"""

        # 4-qubit quantum volume test circuit
        shots = 100
        circuit = quantum_volume_circuit(4, 1, measure=True, seed=0)
        system_memory = int(psutil.virtual_memory().total / 1024 / 1024)

        # Test defaults
        opts = self.BACKEND_OPTS.copy()
        result = execute(circuit, self.SIMULATOR, shots=shots,
                         backend_options=opts).result()
        max_mem_result = result.metadata.get('max_memory_mb')
        self.assertGreaterEqual(max_mem_result, int(system_memory / 2),
                                msg="Default 'max_memory_mb' is too small.")
        self.assertLessEqual(max_mem_result, system_memory,
                             msg="Default 'max_memory_mb' is too large.")

        # Test custom value
        max_mem_target = 128
        opts['max_memory_mb'] = max_mem_target
        result = execute(circuit, self.SIMULATOR, shots=shots,
                         backend_options=opts).result()
        max_mem_result = result.metadata.get('max_memory_mb')
        self.assertEqual(max_mem_result, max_mem_target,
                         msg="Custom 'max_memory_mb' is not being set correctly.")
 def test_qasm_auto_short_shot_parallelization(self):
     """test auto-disabling max_parallel_shots because a number of shots is few"""
     # Test circuit
     shots = multiprocessing.cpu_count() - 1
     if shots == 0:
         return
     circuit = quantum_volume_circuit(4, 1, measure=True, seed=0)
     qobj = compile(circuit, self.SIMULATOR, shots=shots)
     backend_opts = {
         'max_parallel_shots': multiprocessing.cpu_count(),
         'noise_model': self.dummy_noise_model()
     }
     result = self.SIMULATOR.run(qobj,
                                 backend_options=backend_opts).result()
     if result.metadata['omp_enabled']:
         self.assertEqual(result.metadata['parallel_experiments'],
                          1,
                          msg="parallel_experiments should be 1")
         self.assertEqual(
             result.to_dict()['results'][0]['metadata']['parallel_shots'],
             shots,
             msg="parallel_shots must be " + str(shots))
         self.assertEqual(result.to_dict()['results'][0]['metadata']
                          ['parallel_state_update'],
                          int(multiprocessing.cpu_count() / shots),
                          msg="parallel_state_update should be " +
                          str(int(multiprocessing.cpu_count() / shots)))
    def test_qasm_default_parallelization(self):
        """test default parallelization"""
        # Test circuit
        shots = 100
        circuit = quantum_volume_circuit(4, 1, measure=True, seed=0)
        qobj = compile(circuit, self.SIMULATOR, shots=shots)

        backend_opts = self.BACKEND_OPTS.copy()

        result = self.SIMULATOR.run(
            qobj, backend_options=backend_opts).result()
        if result.metadata['omp_enabled']:
            self.assertEqual(
                result.metadata['parallel_experiments'],
                1,
                msg="parallel_experiments should be 1")
            self.assertEqual(
                result.to_dict()['results'][0]['metadata']['parallel_shots'],
                1,
                msg="parallel_shots must be 1")
            self.assertEqual(
                result.to_dict()['results'][0]['metadata']
                ['parallel_state_update'],
                multiprocessing.cpu_count(),
                msg="parallel_state_update should be " + str(
                    multiprocessing.cpu_count()))
Beispiel #11
0
    def test_fusion_qv(self):
        """Test Fusion with quantum volume"""
        shots = 100

        circuit = transpile(quantum_volume_circuit(10, 2, measure=True,
                                                   seed=0),
                            backend=self.SIMULATOR,
                            optimization_level=0)
        qobj = assemble([circuit],
                        self.SIMULATOR,
                        shots=shots,
                        seed_simulator=1)

        backend_options = self.fusion_options(enabled=False, threshold=1)
        result_disabled = self.SIMULATOR.run(
            qobj, backend_options=backend_options).result()
        meta_disabled = self.fusion_metadata(result_disabled)

        backend_options = self.fusion_options(enabled=True, threshold=1)
        result_enabled = self.SIMULATOR.run(
            qobj, backend_options=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.assertEqual(meta_disabled, {})
        self.assertTrue(meta_enabled.get('applied', False))
        self.assertDictAlmostEqual(result_enabled.get_counts(circuit),
                                   result_disabled.get_counts(circuit),
                                   delta=0.0,
                                   msg="fusion for qft was failed")
    def test_qasm_auto_enable_parallel_experiments(self):
        """test default max memory configuration"""
        # Test circuit
        shots = 100
        experiments = multiprocessing.cpu_count()
        circuits = []
        for _ in range(experiments):
            circuits.append(quantum_volume_circuit(4, 1, measure=True, seed=0))

        backend_opts = self.BACKEND_OPTS.copy()
        backend_opts['max_parallel_experiments'] = experiments
        backend_opts['max_memory_mb'] = 1024

        result = execute(circuits,
                         self.SIMULATOR,
                         shots=shots,
                         backend_options=backend_opts).result()
        if result.metadata['omp_enabled']:
            self.assertEqual(
                result.metadata['parallel_experiments'],
                multiprocessing.cpu_count(),
                msg="parallel_experiments should be multiprocessing.cpu_count()"
            )
            self.assertEqual(
                result.to_dict()['results'][0]['metadata']['parallel_shots'],
                1,
                msg="parallel_shots must be 1")
            self.assertEqual(result.to_dict()['results'][0]['metadata']
                             ['parallel_state_update'],
                             1,
                             msg="parallel_state_update should be 1")
 def test_qasm_max_memory_specified(self):
     """test default max memory configuration"""
     # Test circuit
     shots = 100
     circuit = quantum_volume_circuit(4, 1, measure=True, seed=0)
     qobj = compile(circuit, self.SIMULATOR, shots=shots)
     backend_opts = {'max_memory_mb': 128}
     result = self.SIMULATOR.run(qobj,
                                 backend_options=backend_opts).result()
     self.assertEqual(result.metadata['max_memory_mb'],
                      128,
                      msg="max_memory_mb is not configured correctly.")
 def test_qasm_max_memory_default(self):
     """test default max memory"""
     # Test circuit
     shots = 100
     circuit = quantum_volume_circuit(4, 1, measure=True, seed=0)
     qobj = compile(circuit, self.SIMULATOR, shots=shots)
     backend_opts = {}
     result = self.SIMULATOR.run(qobj,
                                 backend_options=backend_opts).result()
     system_memory = int(psutil.virtual_memory().total / 1024 / 1024)
     self.assertGreaterEqual(result.metadata['max_memory_mb'],
                             int(system_memory / 2),
                             msg="statevector_memory is too small.")
     self.assertLessEqual(result.metadata['max_memory_mb'],
                          system_memory,
                          msg="statevector_memory is too big.")
    def test_qasm_max_memory_specified(self):
        """test default max memory configuration"""
        # Test circuit
        shots = 100
        circuit = quantum_volume_circuit(4, 1, measure=True, seed=0)

        backend_opts = self.BACKEND_OPTS.copy()
        backend_opts['max_memory_mb'] = 128

        result = execute(circuit,
                         self.SIMULATOR,
                         shots=shots,
                         backend_options=backend_opts).result()
        self.assertEqual(result.metadata['max_memory_mb'],
                         128,
                         msg="max_memory_mb is not configured correctly.")
Beispiel #16
0
    def test_fusion_qv(self):
        """Test Fusion with quantum volume"""
        shots = 100

        circuit = quantum_volume_circuit(10, 1, measure=True, seed=0)
        qobj = assemble([circuit],
                        self.SIMULATOR,
                        shots=shots,
                        seed_simulator=1)

        backend_options = self.BACKEND_OPTS.copy()
        backend_options['fusion_enable'] = True
        backend_options['fusion_verbose'] = True
        backend_options['fusion_threshold'] = 1
        backend_options['optimize_ideal_threshold'] = 1
        backend_options['optimize_noise_threshold'] = 1

        result_fusion = self.SIMULATOR.run(
            qobj, backend_options=backend_options).result()
        self.assertTrue(getattr(result_fusion, 'success', 'False'))

        backend_options = self.BACKEND_OPTS.copy()
        backend_options['fusion_enable'] = False
        backend_options['fusion_verbose'] = True
        backend_options['fusion_threshold'] = 1
        backend_options['optimize_ideal_threshold'] = 1
        backend_options['optimize_noise_threshold'] = 1

        result_nonfusion = self.SIMULATOR.run(
            qobj, backend_options=backend_options).result()
        self.assertTrue(getattr(result_nonfusion, 'success', 'False'))

        self.assertDictAlmostEqual(result_fusion.get_counts(circuit),
                                   result_nonfusion.get_counts(circuit),
                                   delta=0.0,
                                   msg="fusion for qv was failed")
Beispiel #17
0
    def test_parallel_shot_thread_assignment(self):
        """Test parallel shot thread assignment"""

        max_threads = self.available_threads()
        opts = self.BACKEND_OPTS.copy()
        opts['max_parallel_shots'] = max_threads

        # Test single circuit
        # Parallel experiments and shots should always be 1
        result = execute(self.dummy_circuit(1),
                         self.SIMULATOR,
                         shots=10*max_threads,
                         backend_options=opts).result()
        for threads in self.threads_used(result):
            target = {
                'experiments': 1,
                'shots': 1,
                'state_update': max_threads,
                'total': max_threads
            }
            self.assertEqual(threads, target)

        # Test multiple circuit, no noise
        # Parallel experiments and shots should always be 1
        result = execute(max_threads*[self.dummy_circuit(1)],
                         self.SIMULATOR,
                         shots=10*max_threads,
                         backend_options=opts).result()
        for threads in self.threads_used(result):
            target = {
                'experiments': 1,
                'shots': 1,
                'state_update': max_threads,
                'total': max_threads
            }
            self.assertEqual(threads, target)

        # Test multiple circuits, with noise
        # Parallel shots should take priority
        result = execute(max_threads*[self.dummy_circuit(1)],
                         self.SIMULATOR,
                         shots=10*max_threads,
                         noise_model=self.dummy_noise_model(),
                         backend_options=opts).result()
        for threads in self.threads_used(result):
            target = {
                'experiments': 1,
                'shots': max_threads,
                'state_update': 1,
                'total': max_threads
            }
            self.assertEqual(threads, target)

        # Test multiple circuit, with measure in middle, no noise
        # Parallel shots should take priority
        result = execute(max_threads*[self.measure_in_middle_circuit(1)],
                         self.SIMULATOR,
                         shots=10*max_threads,
                         noise_model=self.dummy_noise_model(),
                         backend_options=opts).result()
        for threads in self.threads_used(result):
            target = {
                'experiments': 1,
                'shots': max_threads,
                'state_update': 1,
                'total': max_threads
            }
            self.assertEqual(threads, target)

        # Test multiple circuits, with memory limitation
        # NOTE: this assumes execution on statevector simulator
        # which required approx 2 MB for 16 qubit circuit.
        opts['max_memory_mb'] = 1
        result = execute(2 * [quantum_volume_circuit(16, 1, measure=True, seed=0)],
                         self.SIMULATOR,
                         shots=10*max_threads,
                         backend_options=opts).result()
        for threads in self.threads_used(result):
            target = {
                'experiments': 1,
                'shots': 1,
                'state_update': max_threads,
                'total': max_threads
            }
            self.assertEqual(threads, target)