Skip to content

Commit

Permalink
use aer_simulator correctly in benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
hhorii committed Nov 11, 2021
1 parent b2948af commit 2c249cb
Showing 1 changed file with 15 additions and 52 deletions.
67 changes: 15 additions & 52 deletions test/benchmark/simulator_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,28 +118,28 @@ def __init__(self,
self.noise_models[self.NOISE_DEPOLARIZING] = noise_model

if self.RUNTIME_STATEVECTOR_CPU in runtime_names:
self.simulators[self.RUNTIME_STATEVECTOR_CPU] = SIMULATOR
self.backend_options_list[self.RUNTIME_STATEVECTOR_CPU] = { 'method': self.RUNTIME_STATEVECTOR_CPU, 'enable_truncation': False}
self.simulators[self.RUNTIME_STATEVECTOR_CPU] = AerSimulator(method='statevector', device='CPU')
self.backend_options_list[self.RUNTIME_STATEVECTOR_CPU] = { 'enable_truncation': False}
self.backend_qubits[self.RUNTIME_STATEVECTOR_CPU] = self.qubits

if self.RUNTIME_STATEVECTOR_GPU in runtime_names:
self.simulators[self.RUNTIME_STATEVECTOR_GPU] = SIMULATOR
self.backend_options_list[self.RUNTIME_STATEVECTOR_GPU] = { 'method': self.RUNTIME_STATEVECTOR_CPU, 'device': 'GPU', 'enable_truncation': False}
self.simulators[self.RUNTIME_STATEVECTOR_GPU] = AerSimulator(method='statevector', device='GPU')
self.backend_options_list[self.RUNTIME_STATEVECTOR_GPU] = { 'enable_truncation': False}
self.backend_qubits[self.RUNTIME_STATEVECTOR_GPU] = self.qubits

if self.RUNTIME_MPS_CPU in runtime_names:
self.simulators[self.RUNTIME_MPS_CPU] = SIMULATOR
self.backend_options_list[self.RUNTIME_MPS_CPU] = { 'method': self.RUNTIME_MPS_CPU, 'enable_truncation': False}
self.simulators[self.RUNTIME_MPS_CPU] = AerSimulator(method='matrix_product_state', device='GPU')
self.backend_options_list[self.RUNTIME_MPS_CPU] = { 'enable_truncation': False}
self.backend_qubits[self.RUNTIME_MPS_CPU] = self.qubits

if self.RUNTIME_DENSITY_MATRIX_CPU in runtime_names:
self.simulators[self.RUNTIME_DENSITY_MATRIX_CPU] = SIMULATOR
self.backend_options_list[self.RUNTIME_DENSITY_MATRIX_CPU] = { 'method': self.RUNTIME_DENSITY_MATRIX_CPU, 'enable_truncation': False}
self.simulators[self.RUNTIME_DENSITY_MATRIX_CPU] = AerSimulator(method='density_matrix', device='CPU')
self.backend_options_list[self.RUNTIME_DENSITY_MATRIX_CPU] = { 'enable_truncation': False}
self.backend_qubits[self.RUNTIME_DENSITY_MATRIX_CPU] = [qubit for qubit in qubits if qubit <= 15]

if self.RUNTIME_DENSITY_MATRIX_GPU in runtime_names:
self.simulators[self.RUNTIME_DENSITY_MATRIX_GPU] = SIMULATOR
self.backend_options_list[self.RUNTIME_DENSITY_MATRIX_GPU] = { 'method': self.RUNTIME_DENSITY_MATRIX_CPU, 'device': 'GPU', 'enable_truncation': False}
self.simulators[self.RUNTIME_DENSITY_MATRIX_GPU] = AerSimulator(method='density_matrix', device='GPU')
self.backend_options_list[self.RUNTIME_DENSITY_MATRIX_GPU] = { 'enable_truncation': False}
self.backend_qubits[self.RUNTIME_DENSITY_MATRIX_GPU] = [qubit for qubit in qubits if qubit <= 15]

def gen_qobj(self, runtime, app, measure, measure_count, qubit):
Expand Down Expand Up @@ -167,49 +167,12 @@ def add_expval(base, num_terms):
circuit = circuit.bind_parameters(param_binds)

simulator = self.simulators[runtime]
if measure == self.MEASUREMENT_SAMPLING:
if runtime in self.TRANSPLIERS:
runtime_circuit = eval(self.TRANSPLIERS[runtime])(circuit)
if (runtime, app, measure, measure_count, qubit) not in QOBJS:
QOBJS[(runtime, app, measure, measure_count, qubit)] = assemble(runtime_circuit, simulator, shots=measure_count)
return QOBJS[(runtime, app, measure, measure_count, qubit)]
else:
runtime_circuit = circuit
if (simulator, app, measure, measure_count, qubit) not in QOBJS:
QOBJS[(simulator, app, measure, measure_count, qubit)] = assemble(runtime_circuit, simulator, shots=measure_count)
return QOBJS[(simulator, app, measure, measure_count, qubit)]
elif measure == self.MEASUREMENT_EXPVAL:
if runtime in self.TRANSPLIERS:
runtime_circuit = eval(self.TRANSPLIERS[runtime])(circuit)
if (runtime, app, measure, measure_count, qubit) not in QOBJS:
QOBJS[(runtime, app, measure, measure_count, qubit)] = assemble(runtime_circuit, simulator, shots=1)
return QOBJS[(runtime, app, measure, measure_count, qubit)]
else:
runtime_circuit = circuit
if (simulator, app, measure, measure_count, qubit) not in QOBJS:
QOBJS[(simulator, app, measure, measure_count, qubit)] = assemble(runtime_circuit, simulator, shots=1)
return QOBJS[(simulator, app, measure, measure_count, qubit)]
shots = measure_count if measure == self.MEASUREMENT_SAMPLING else 1

if (simulator, app, measure, measure_count, qubit) not in QOBJS:
QOBJS[(runtime, app, measure, measure_count, qubit)] = assemble(transpile(circuit, simulator), simulator, shots=shots)
return QOBJS[(simulator, app, measure, measure_count, qubit)]

def _transpile(self, circuit, basis_gates):
from qiskit import transpile
return transpile(circuit, basis_gates=basis_gates)

def transpile(self, circuit):
return self._transpile(circuit, [
'u1', 'u2', 'u3', 'cx', 'cz', 'id', 'x', 'y', 'z', 'h', 's', 'sdg',
't', 'tdg', 'swap', 'ccx', 'unitary', 'diagonal', 'initialize',
'cu1', 'cu2', 'cu3', 'cswap', 'mcx', 'mcy', 'mcz',
'mcu1', 'mcu2', 'mcu3', 'mcswap', 'multiplexer', 'kraus', 'roerror'])

def transpile_for_mps(self, circuit):
return self._transpile(circuit, [
'u1', 'u2', 'u3', 'cx', 'cz', 'id', 'x', 'y', 'z', 'h', 's', 'sdg',
't', 'tdg', 'swap', 'ccx'#, 'unitary', 'diagonal', 'initialize',
'cu1', #'cu2', 'cu3', 'cswap', 'mcx', 'mcy', 'mcz',
#'mcu1', 'mcu2', 'mcu3', 'mcswap', 'multiplexer', 'kraus',
'roerror'
])

def _run(self, runtime, app, measure, measure_count, noise_name, qubit):
if runtime not in self.simulators or runtime not in self.backend_options_list:
raise ValueError('unknown runtime: {0}'.format(runtime))
Expand Down

0 comments on commit 2c249cb

Please sign in to comment.