def test_max_memory_settings(self): """test max memory configuration""" # 4-qubit quantum circuit shots = 100 circuit = QuantumVolume(4, 1, seed=0) circuit.measure_all() system_memory = int(psutil.virtual_memory().total / 1024 / 1024) # Test defaults opts = self.backend_options_parallel() result = execute(circuit, self.SIMULATOR, shots=shots, **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 = self.backend_options_parallel() opts['max_memory_mb'] = max_mem_target result = execute(circuit, self.SIMULATOR, shots=shots, **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_explicit_parallelization(self): """test disabling parallel shots because max_parallel_shots is 1""" # Test circuit shots = multiprocessing.cpu_count() circuit = QuantumVolume(16, 1, seed=0) circuit.measure_all() backend_opts = self.backend_options_parallel(shot_threads=1, exp_threads=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")
class QiskitTranspilerBenchmarks: params = [0, 1, 2, 3] param_names = ["transpiler optimization level"] version = "0.15.1" timeout = 240 def setup(self, _): self.backend = FakeRochester() self.circuit = QuantumVolume(53, seed=42) self.circuit.measure_all() def time_transpile(self, level): transpile( self.circuit, backend=self.backend, seed_transpiler=4242, optimization_level=level, ) def peakmem_transpile(self, level): transpile( self.circuit, backend=self.backend, seed_transpiler=4242, optimization_level=level, )
def circuits(self, backend: Optional[Backend] = None) -> List[QuantumCircuit]: """Return a list of Quantum Volume circuits. Args: backend (Backend): Optional, a backend object. Returns: A list of :class:`QuantumCircuit`. """ circuits = [] depth = self._num_qubits # Note: the trials numbering in the metadata is starting from 1 for each new experiment run for trial in range(1, self.experiment_options.trials + 1): qv_circ = QuantumVolumeCircuit(depth, depth, seed=self._rng) qv_circ.measure_active() qv_circ.metadata = { "experiment_type": self._type, "depth": depth, "trial": trial, "qubits": self.physical_qubits, "ideal_probabilities": self._get_ideal_data(qv_circ), } circuits.append(qv_circ) return circuits
def test_mps_measure_alg_qv(self): """Test MPS measure algorithms with quantum volume""" backend = self.backend(method="matrix_product_state") shots = 1000 n = 5 depth = 2 circuit = QuantumVolume(n, depth, seed=9) circuit.measure_all() circuit = transpile(circuit, backend) result1 = backend.run( circuit, shots=shots, mps_sample_measure_algorithm="mps_apply_measure").result() self.assertTrue(getattr(result1, 'success', 'True')) result2 = backend.run( circuit, shots=shots, mps_sample_measure_algorithm="mps_probabilities").result() self.assertTrue(getattr(result2, 'success', 'True')) self.assertDictAlmostEqual(result1.get_counts(circuit), result2.get_counts(circuit), delta=0.1 * shots)
def test_chunk_QuantumVolumeWithFusion(self): """Test multi-chunk with fused quantum volume""" shots = 100 num_qubits = 8 depth = 10 backend_options = self.BACKEND_OPTS.copy() backend_options['fusion_enable'] = True backend_options['fusion_threshold'] = 5 backend_options["blocking_qubits"] = 4 backend_options_no_chunk = self.BACKEND_OPTS.copy() backend_options_no_chunk.pop("blocking_enable") backend_options_no_chunk.pop("blocking_qubits") backend_options_no_chunk['fusion_enable'] = True backend_options_no_chunk['fusion_threshold'] = 5 circuit = transpile(QuantumVolume(num_qubits, depth, seed=0), backend=self.SIMULATOR, optimization_level=0) circuit.measure_all() qobj = assemble(circuit, shots=shots, memory=True) result = self.SIMULATOR.run(qobj, **backend_options_no_chunk).result() counts_no_chunk = result.get_counts(circuit) result = self.SIMULATOR.run(qobj, **backend_options).result() counts = result.get_counts(circuit) self.assertEqual(counts_no_chunk,counts)
def test_save_expval_var_nonstabilizer_pauli(self, pauli): """Test Pauli expval_var for non-stabilizer circuit""" SUPPORTED_METHODS = [ 'automatic', 'statevector', 'statevector_gpu', 'statevector_thrust', 'density_matrix', 'density_matrix_gpu', 'density_matrix_thrust', 'matrix_product_state', 'extended_stabilizer' ] SEED = 7382 # Stabilizer test circuit state_circ = QuantumVolume(2, 1, seed=SEED) oper = qi.Operator(qi.Pauli(pauli)) state = qi.Statevector(state_circ) expval = state.expectation_value(oper).real variance = state.expectation_value(oper**2).real - expval**2 target = [expval, variance] # Snapshot circuit opts = self.BACKEND_OPTS.copy() method = opts.get('method', 'automatic') circ = transpile(state_circ, basis_gates=['u1', 'u2', 'u3', 'cx', 'swap']) circ.save_expectation_value_variance(oper, [0, 1], label='expval') qobj = assemble(circ) result = self.SIMULATOR.run(qobj, **opts).result() if method not in SUPPORTED_METHODS: self.assertFalse(result.success) else: self.assertTrue(result.success) value = result.data(0)['expval'] self.assertTrue(allclose(value, target))
def test_fusion_parallelization(self): """Test Fusion parallelization option""" shots = 100 num_qubits = 8 depth = 2 parallelization = 2 backend = self.backend(method="statevector") circuit = transpile(QuantumVolume(num_qubits, depth, seed=0), backend=backend, optimization_level=0) circuit.measure_all() options_serial = self.fusion_options(enabled=True, threshold=1, parallelization=1) result_serial = backend.run(circuit, shots=shots, **options_serial).result() meta_serial = self.fusion_metadata(result_serial) options_parallel = self.fusion_options(enabled=True, threshold=1, parallelization=2) result_parallel = backend.run(circuit, shots=shots, **options_parallel).result() meta_parallel = self.fusion_metadata(result_parallel) self.assertTrue(getattr(result_serial, 'success', 'False')) self.assertTrue(getattr(result_parallel, 'success', 'False')) self.assertEqual(meta_serial.get('parallelization'), 1) self.assertEqual(meta_parallel.get('parallelization'), parallelization) self.assertDictAlmostEqual(result_parallel.get_counts(circuit), result_serial.get_counts(circuit), delta=0.0, msg="parallelized fusion was failed")
def test_fusion_qv(self): """Test Fusion with quantum volume""" shots = 100 num_qubits = 6 depth = 2 circuit = transpile(QuantumVolume(num_qubits, depth, seed=0), backend=self.SIMULATOR, optimization_level=0) circuit.measure_all() qobj_disabled = assemble([circuit], self.SIMULATOR, shots=shots, **self.fusion_options(enabled=False, threshold=1, verbose=True)) result_disabled = self.SIMULATOR.run(qobj_disabled).result() meta_disabled = self.fusion_metadata(result_disabled) qobj_enabled = assemble([circuit], self.SIMULATOR, shots=shots, **self.fusion_options(enabled=True, threshold=1, verbose=True)) result_enabled = self.SIMULATOR.run(qobj_enabled).result() meta_enabled = self.fusion_metadata(result_enabled) self.assertTrue(getattr(result_disabled, 'success', 'False')) self.assertTrue(getattr(result_enabled, 'success', 'False')) self.assertFalse(meta_disabled.get('applied', False)) 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_save_unitary(self): """Test save unitary for instruction""" SUPPORTED_METHODS = [ 'automatic', 'unitary', 'unitary_gpu', 'unitary_thrust' ] # Stabilizer test circuit circ = transpile(QuantumVolume(3), self.SIMULATOR) # Target unitary target = qi.Operator(circ) # Add save to circuit save_key = 'state' circ.save_unitary(save_key) # Run opts = self.BACKEND_OPTS.copy() qobj = assemble(circ, self.SIMULATOR) result = self.SIMULATOR.run(qobj, **opts).result() method = opts.get('method', 'automatic') if method not in SUPPORTED_METHODS: self.assertFalse(result.success) else: self.assertTrue(result.success) data = result.data(0) self.assertIn(save_key, data) value = qi.Operator(result.data(0)[save_key]) self.assertEqual(value, target)
def test_save_expval_nonstabilizer_hermitian(self, qubits): """Test expval for non-stabilizer circuit and Hermitian operator""" SUPPORTED_METHODS = [ 'automatic', 'statevector', 'statevector_gpu', 'statevector_thrust', 'density_matrix', 'density_matrix_gpu', 'density_matrix_thrust', 'matrix_product_state' ] SEED = 8124 # Stabilizer test circuit state = QuantumVolume(3, 1, seed=SEED) oper = qi.random_hermitian(4, traceless=True, seed=SEED) target = qi.Statevector(state).expectation_value(oper, qubits).real # Snapshot circuit opts = self.BACKEND_OPTS.copy() circ = transpile(state, self.SIMULATOR) circ.save_expectation_value('expval', oper, qubits) qobj = assemble(circ) result = self.SIMULATOR.run(qobj, **opts).result() method = opts.get('method', 'automatic') if method not in SUPPORTED_METHODS: self.assertFalse(result.success) else: self.assertTrue(result.success) value = result.data(0)['expval'] self.assertAlmostEqual(value, target)
def test_chunk_QuantumVolumeWithFusion(self, method, device): """Test multi-chunk with fused quantum volume""" opts_no_chunk = { "fusion_enable": True, "fusion_threshold": 5, } opts_chunk = copy.copy(opts_no_chunk) opts_chunk["blocking_enable"] = True opts_chunk["blocking_qubits"] = 4 backend = self.backend( method=method, device=device, **opts_chunk) backend_no_chunk = self.backend( method=method, device=device, **opts_no_chunk) shots = 100 num_qubits = 8 depth = 10 circuit = transpile(QuantumVolume(num_qubits, depth, seed=0), backend=backend, optimization_level=0) circuit.measure_all() result = backend.run(circuit, shots=shots, memory=True).result() counts = result.get_counts(circuit) result_no_chunk = backend_no_chunk.run(circuit, shots=shots, memory=True).result() counts_no_chunk = result_no_chunk.get_counts(circuit) self.assertEqual(counts_no_chunk, counts)
def test_fusion_qv(self): """Test Fusion with quantum volume""" shots = 100 num_qubits = 6 depth = 2 circuit = transpile(QuantumVolume(num_qubits, depth, seed=0), backend=self.SIMULATOR, optimization_level=0) circuit.measure_all() qobj_disabled = assemble([circuit], self.SIMULATOR, shots=shots, **self.fusion_options(enabled=False, threshold=1, verbose=True)) result_disabled = self.SIMULATOR.run(qobj_disabled).result() #meta_disabled = self.fusion_metadata(result_disabled) qobj_enabled = assemble([circuit], self.SIMULATOR, shots=shots, **self.fusion_options(enabled=True, threshold=1, verbose=True)) result_enabled = self.SIMULATOR.run(qobj_enabled).result()
def test_save_expval_var_nonstabilizer_hermitian(self, qubits): """Test expval_var for non-stabilizer circuit and Hermitian operator""" SUPPORTED_METHODS = [ 'automatic', 'statevector', 'statevector_gpu', 'statevector_thrust', 'density_matrix', 'density_matrix_gpu', 'density_matrix_thrust', 'matrix_product_state' ] SEED = 8124 # Stabilizer test circuit state_circ = QuantumVolume(3, 1, seed=SEED) oper = qi.random_hermitian(4, traceless=True, seed=SEED) state = qi.Statevector(state_circ) expval = state.expectation_value(oper, qubits).real variance = state.expectation_value(oper ** 2, qubits).real - expval ** 2 target = [expval, variance] # Snapshot circuit opts = self.BACKEND_OPTS.copy() method = opts.get('method', 'automatic') circ = transpile(state_circ, basis_gates=['u1', 'u2', 'u3', 'cx', 'swap']) circ.save_expectation_value_variance('expval', oper, qubits) qobj = assemble(circ) result = self.SIMULATOR.run(qobj, **opts).result() if method not in SUPPORTED_METHODS: self.assertFalse(result.success) else: self.assertTrue(result.success) value = result.data(0)['expval'] self.assertTrue(allclose(value, target))
def test_save_expval_nonstabilizer_pauli(self, pauli): """Test Pauli expval for non-stabilizer circuit""" SUPPORTED_METHODS = [ 'automatic', 'statevector', 'statevector_gpu', 'statevector_thrust', 'density_matrix', 'density_matrix_gpu', 'density_matrix_thrust', 'matrix_product_state' ] SEED = 7382 # Stabilizer test circuit state_circ = QuantumVolume(2, 1, seed=SEED) oper = qi.Pauli(pauli) state = qi.Statevector(state_circ) target = state.expectation_value(oper).real.round(10) # Snapshot circuit opts = self.BACKEND_OPTS.copy() method = opts.get('method', 'automatic') circ = transpile(state_circ, basis_gates=['u1', 'u2', 'u3', 'cx', 'swap']) circ.save_expectation_value('expval', oper, [0, 1]) qobj = assemble(circ) result = self.SIMULATOR.run(qobj, **opts).result() if method not in SUPPORTED_METHODS: self.assertFalse(result.success) else: self.assertTrue(result.success) value = result.data(0)['expval'] self.assertAlmostEqual(value, target)
def test_auto_method_partial_result_a_single_invalid_circuit(self): """Test a partial result is returned with a job with a valid and invalid circuit.""" circuits = [] qc = QuantumCircuit(2) qc.h(0) qc.cx(0, 1) qc.measure_all() qc_2 = QuantumVolume(5) qc_2.measure_all() circuits.append(qc_2) circuits.append(qc) backend = self.backend() shots = 100 result = backend.run(circuits, shots=shots).result() self.assertEqual(result.status, 'PARTIAL COMPLETED') self.assertTrue(hasattr(result.results[1].data, 'counts')) self.assertFalse(hasattr(result.results[0].data, 'counts'))
def test_mps_measure_alg_qv(self): """Test MPS measure algorithms with quantum volume""" shots = 1000 n = 5 depth = 2 circuit = QuantumVolume(n, depth, seed=9) circuit.measure_all() result1 = execute(circuit, self.SIMULATOR, shots=shots, **self.BACKEND_OPTS1).result() self.assertTrue(getattr(result1, 'success', 'True')) result2 = execute(circuit, self.SIMULATOR, shots=shots, **self.BACKEND_OPTS2).result() self.assertTrue(getattr(result2, 'success', 'True')) self.assertDictAlmostEqual(result1.get_counts(circuit), result2.get_counts(circuit), delta=0.1 * shots)
def test_qv(self): """Test qv circuit.""" circuit = QuantumVolume(2, 2, seed=2, classical_permutation=False) expected = QuantumCircuit(2) expected.swap(0, 1) expected.append(random_unitary(4, seed=837), [0, 1]) expected.append(random_unitary(4, seed=262), [0, 1]) expected = Operator(expected) simulated = Operator(circuit) self.assertTrue(expected.equiv(simulated))
def test_save_expval_nonstabilizer_hermitian(self, method, device, qubits): """Test expval for non-stabilizer circuit and Hermitian operator""" SEED = 8124 circ = QuantumVolume(3, 1, seed=SEED) oper = qi.random_hermitian(4, traceless=True, seed=SEED) self._test_save_expval(circ, oper, qubits, False, method=method, device=device)
def test_save_expval_var_nonstabilizer_pauli(self, method, device, pauli): """Test Pauli expval_var for non-stabilizer circuit""" SEED = 7382 circ = QuantumVolume(2, 1, seed=SEED) oper = qi.Operator(qi.Pauli(pauli)) qubits = [0, 1] self._test_save_expval(circ, oper, qubits, True, method=method, device=device)
def test_max_memory_settings(self): """test max memory configuration""" backend = self.backend(**self.backend_options_parallel()) circuit = transpile(QuantumVolume(4, 1, seed=0), backend) circuit.measure_all() system_memory = int(psutil.virtual_memory().total / 1024 / 1024) result = backend.run(circuit, shots=100).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.")
def test_custom_memory_settings(self): """test max memory configuration""" max_mem_target = 128 backend = self.backend( max_memory_mb=max_mem_target, **self.backend_options_parallel()) circuit = transpile(QuantumVolume(4, 1, seed=0), backend) circuit.measure_all() result = backend.run(circuit, shots=100).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_fusion_theshold(self): """Test fusion threhsold settings work.""" seed = 12345 threshold = 4 backend_options = self.fusion_options(enabled=True, threshold=threshold) with self.subTest(msg='below fusion threshold'): circuit = QuantumVolume(threshold - 1, seed=seed) circuit = transpile(circuit, self.SIMULATOR) qobj = assemble([circuit], shots=1) result = self.SIMULATOR.run( qobj, backend_options=backend_options).result() meta = self.fusion_metadata(result) self.assertSuccess(result) self.assertFalse(meta.get('applied')) with self.subTest(msg='at fusion threshold'): circuit = QuantumVolume(threshold, seed=seed) circuit = transpile(circuit, self.SIMULATOR) qobj = assemble([circuit], shots=1) result = self.SIMULATOR.run( qobj, backend_options=backend_options).result() meta = self.fusion_metadata(result) self.assertSuccess(result) self.assertTrue(meta.get('applied')) with self.subTest(msg='above fusion threshold'): circuit = QuantumVolume(threshold + 1, seed=seed) circuit = transpile(circuit, self.SIMULATOR) qobj = assemble([circuit], shots=1) result = self.SIMULATOR.run( qobj, backend_options=backend_options).result() meta = self.fusion_metadata(result) self.assertSuccess(result) self.assertTrue(meta.get('applied'))
def test_save_unitary(self, method, device): """Test save unitary instruction""" backend = self.backend(method=method, device=device) # Test circuit SEED = 5426 circ = QuantumVolume(3, seed=SEED) # Target unitary target = qi.Operator(circ) # Add save to circuit label = 'state' circ.save_unitary(label=label) # Run result = backend.run(transpile( circ, backend, optimization_level=0), shots=1).result() self.assertTrue(result.success) simdata = result.data(0) self.assertIn(label, simdata) value = qi.Operator(simdata[label]) self.assertEqual(value, target)
def test_MPI_QuantumVolume(self): """Test MPI with quantum volume""" shots = 100 num_qubits = 10 depth = 10 backend_options = self.BACKEND_OPTS.copy() circuit = transpile(QuantumVolume(num_qubits, depth, seed=0), backend=self.SIMULATOR, optimization_level=0) circuit.measure_all() qobj = assemble(circuit, shots=shots, memory=True) result = self.SIMULATOR.run(qobj, **backend_options).result() counts = result.get_counts(circuit) #comparing counts with pre-computed counts self.assertEqual(counts, self.ref_counts_qv10)
def test_fusion_output(self): """Test Fusion returns same final unitary""" seed = 54321 circuit = QuantumVolume(4, seed=seed) circuit = transpile(circuit, self.SIMULATOR) qobj = assemble([circuit], shots=1) options_disabled = self.fusion_options(enabled=False, threshold=1) result_disabled = self.SIMULATOR.run(qobj, **options_disabled).result() self.assertSuccess(result_disabled) options_enabled = self.fusion_options(enabled=True, threshold=1) result_enabled = self.SIMULATOR.run(qobj, **options_enabled).result() self.assertSuccess(result_enabled) sv_no_fusion = Statevector(result_disabled.get_statevector(0)) sv_fusion = Statevector(result_enabled.get_statevector(0)) self.assertEqual(sv_no_fusion, sv_fusion)
def test_fusion_disable(self): """Test Fusion enable/disable option""" seed = 2233 circuit = QuantumVolume(4, seed=seed) circuit = transpile(circuit, self.SIMULATOR) qobj = assemble([circuit], shots=1) with self.subTest(msg='test fusion enable'): backend_options = self.fusion_options(enabled=True, threshold=1) result = self.SIMULATOR.run(qobj, **backend_options).result() meta = self.fusion_metadata(result) self.assertSuccess(result) self.assertTrue(meta.get('applied')) with self.subTest(msg='test fusion disable'): backend_options = self.fusion_options(enabled=False, threshold=1) result = self.SIMULATOR.run(qobj, **backend_options).result() meta = self.fusion_metadata(result) self.assertSuccess(result) self.assertFalse(meta.get('applied'))
def test_parallel_shot_thread_mem_limit(self): """Test parallel shot thread assignment""" max_threads = self.available_threads() backend = self.backend( max_memory_mb=1, **self.backend_options_parallel(shot_threads=max_threads)) # Test multiple circuits, with memory limitation # NOTE: this assumes execution on statevector simulator # which required approx 2 MB for 16 qubit circuit. circuit = transpile(QuantumVolume(16, 1, seed=0), backend) circuit.measure_all() result = backend.run( 2*[circuit], shots=10*max_threads).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': 1, 'state_update': max_threads, 'total': max_threads } self.assertEqual(threads, target)
def test_chunk_QuantumVolume(self, method, device): """Test multi-chunk with quantum volume""" opts = {"blocking_enable": True, "blocking_qubits": 2} backend = self.backend(method=method, device=device, **opts) backend_no_chunk = self.backend(method=method, device=device) shots = 100 num_qubits = 4 depth = 10 circuit = transpile(QuantumVolume(num_qubits, depth, seed=0), backend=backend, optimization_level=0) circuit.measure_all() result = backend.run(circuit, shots=shots, memory=True).result() counts = result.get_counts(circuit) result_no_chunk = backend_no_chunk.run(circuit, shots=shots, memory=True).result() counts_no_chunk = result_no_chunk.get_counts(circuit) self.assertEqual(counts_no_chunk, counts)
def test_fusion_parallelization(self): """Test Fusion parallelization option""" shots = 100 num_qubits = 8 depth = 2 parallelization = 2 circuit = transpile(QuantumVolume(num_qubits, depth, seed=0), 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, parallelization=1) result_serial = self.SIMULATOR.run(qobj, **backend_options).result() meta_serial = self.fusion_metadata(result_serial) backend_options = self.fusion_options(enabled=True, threshold=1, parallelization=2) result_parallel = self.SIMULATOR.run(qobj, **backend_options).result() meta_parallel = self.fusion_metadata(result_parallel) self.assertTrue(getattr(result_serial, 'success', 'False')) self.assertTrue(getattr(result_parallel, 'success', 'False')) self.assertEqual(meta_serial.get('parallelization'), 1) self.assertEqual(meta_parallel.get('parallelization'), parallelization) self.assertDictAlmostEqual(result_parallel.get_counts(circuit), result_serial.get_counts(circuit), delta=0.0, msg="parallelized fusion was failed")