class TestQasmSimulator(MyTestCase): """ Test the QASM Simulator """ def setUp(self): self.seed = 23423456 self.sim = QCGPUProvider().get_backend('qasm_simulator') qasm_file = 'tests/example.qasm' circ = QuantumCircuit.from_qasm_file(qasm_file) self.qobj = compile(circ, backend=self.sim) def test_qasm_simulator_single_show(self): shots = 1 self.qobj.config.shots = shots result = self.sim.run(self.qobj).result() self.assertEqual(result.success, True) def test_qasm_simulator(self): """Test data counts output for single circuit run against reference.""" shots = 1024 self.qobj.config.shots = shots result = self.sim.run(self.qobj).result() threshold = 0.04 * shots counts = result.get_counts() target = { '100 100': shots / 8, '011 011': shots / 8, '101 101': shots / 8, '111 111': shots / 8, '000 000': shots / 8, '010 010': shots / 8, '110 110': shots / 8, '001 001': shots / 8 } self.assertDictAlmostEqual(counts, target, threshold) def test_memory(self): qr = QuantumRegister(4, 'qr') cr0 = ClassicalRegister(2, 'cr0') cr1 = ClassicalRegister(2, 'cr1') circ = QuantumCircuit(qr, cr0, cr1) circ.h(qr[0]) circ.cx(qr[0], qr[1]) circ.x(qr[3]) circ.measure(qr[0], cr0[0]) circ.measure(qr[1], cr0[1]) circ.measure(qr[2], cr1[0]) circ.measure(qr[3], cr1[1]) shots = 50 qobj = compile(circ, backend=self.sim, shots=shots, memory=True) result = self.sim.run(qobj).result() memory = result.get_memory() self.assertEqual(len(memory), shots) for mem in memory: self.assertIn(mem, ['10 00', '10 11'])
def _compare_outcomes(self, circ): Provider = QCGPUProvider() backend_qcgpu = Provider.get_backend('statevector_simulator') statevector_qcgpu = execute(circ, backend_qcgpu).result().get_statevector() backend_qiskit = BasicAer.get_backend('statevector_simulator') statevector_qiskit = execute( circ, backend_qiskit).result().get_statevector() self.assertAlmostEqual( state_fidelity(statevector_qcgpu, statevector_qiskit), 1, 5)
def get_backend(provider_name, backend_name, n_qubits): # logger.debug("real: {0}, online: {1}, backend_name: {2}".format( # args.real, args.online, args.backend_name)) if provider_name == "basicaer": from qiskit import BasicAer provider = BasicAer elif provider_name == "aer": from qiskit import Aer provider = Aer elif provider_name in ("projectqp", "projectqpprovider"): from qiskit_addon_projectq import ProjectQProvider provider = ProjectQProvider() elif provider_name in ("qcgpu", "qcgpuprovider"): from qiskit_qcgpu_provider import QCGPUProvider provider = QCGPUProvider() elif provider_name in ("jku", "jkuprovider"): from qiskit_addon_jku import JKUProvider provider = JKUProvider() elif provider_name in ("ibmq"): from qiskit import IBMQ IBMQ.load_accounts() provider = IBMQ else: raise Exception("Invalid provider {0}".format(provider_name)) # only for real, online execution if backend_name == 'enough' and provider == 'ibmq': from qiskit.providers.ibmq import least_busy large_enough_devices = provider.backends( filters=lambda x: x.configuration( ).n_qubits >= n_qubits and x.configuration().simulator == False) backend = least_busy(large_enough_devices) else: backend = provider.get_backend(backend_name) if backend.configuration().n_qubits < n_qubits: raise Exception( "Backend {0} on provider {1} has only {2} qubits, while {3} are needed." .format(backend.name(), backend.provider(), backend.configuration().n_qubits, n_qubits)) return backend
def setUp(self): self.seed = 23423456 self.sim = QCGPUProvider().get_backend('qasm_simulator') qasm_file = 'tests/example.qasm' circ = QuantumCircuit.from_qasm_file(qasm_file) self.qobj = compile(circ, backend=self.sim)
from qiskit_aqua.utils import split_dataset_to_data_and_labels from qiskit_aqua.input import SVMInput from qiskit_qcgpu_provider import QCGPUProvider from qiskit_aqua import run_algorithm n = 2 # How many features to use (dimensionality) training_dataset_size = 20 testing_dataset_size = 10 sample_Total, training_input, test_input, class_labels = breast_cancer(training_dataset_size, testing_dataset_size, n) datapoints, class_to_label = split_dataset_to_data_and_labels(test_input) print(class_to_label) params = { 'problem': {'name': 'svm_classification', 'random_seed': 10598}, 'algorithm': { 'name': 'QSVM.Kernel' }, 'backend': {'name': 'qasm_simulator', 'shots': 1024}, 'feature_map': {'name': 'SecondOrderExpansion', 'depth': 2, 'entanglement': 'linear'} } backend = QCGPUProvider().get_backend('qasm_simulator') algo_input = SVMInput(training_input, test_input, datapoints[0]) %time result = run_algorithm(params, algo_input) %time result = run_algorithm(params, algo_input, backend=backend) print("ground truth: {}".format(datapoints[1])) print("prediction: {}".format(result['predicted_labels']))
def __init__(self, l, options): """ Quantum Algorithm superclass. Handles execution specifications. options: * seed (int) - Seed for simulator and transpiler. * shots (int) - Number of execution shots. * backend (str) - Name of backend. * device (str) - Name of IBMQ quantum computer. * noise_model (bool) - Noise model of device. * coupling_map (bool) - Coupling map of device. * print (bool) - Print for every function evaluation. * count_states (bool) - Count legal and illegal states for all function evaluations. """ #### Setup options self.options = options # For execution self.shots = 1000 if options.get('shots') == None\ else options.get('shots') self.seed = options.get('seed') if self.seed != None: from qiskit.aqua import aqua_globals aqua_globals.random_seed = self.seed self.prnt = options.get('print') self.ancilla_measure = options.get( 'ancilla') if options.get('ancilla') != None else False self.ibmq = False if options.get('ibmq') == True: print('Running on real quantum computer') self.ibmq = True self.backend = options['backend'] from qiskit.tools.monitor import job_monitor self.monitor = job_monitor from attributes import get_measurement_fitter self.meas_fitter = get_measurement_fitter(l, self.backend, None, self.shots) else: # For Backend if options.get('backend') == None: self.options['backend'] = 'qasm_simulator' self.backend = qk.Aer.get_backend(options['backend']) # For noise model, coupling map and basis gates self.noise_model, self.coupling_map, self.basis_gates = None, None, None self.meas_fitter = None if options.get('device') != None: device = QuantumComputer(options.get('device')) if options.get('noise_model') != None: self.noise_model = device.noise_model # Create error mitigation fitter if options.get('meas_fit') in [None, True]: from attributes import get_measurement_fitter self.meas_fitter = get_measurement_fitter( l, self.backend, device, self.shots) if options.get('coupling_map') != None: self.coupling_map = device.coupling_map if options.get('basis_gates') != None: self.basis_gates = device.basis_gates # Qubit layout, virtual to physical self.layout = options.get('layout') # Optimization level self.optimization_level = 1 if options.get( 'optimization_level') == None else options['optimization_level'] # GPU accelerated if options.get('gpu'): from qiskit_qcgpu_provider import QCGPUProvider Provider = QCGPUProvider() self.backend = Provider.get_backend(options['backend'])
""" In this example a Bell state is made. """ from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister from qiskit import execute from qiskit_qcgpu_provider import QCGPUProvider Provider = QCGPUProvider() # Create a Quantum Register with 2 qubits. q = QuantumRegister(2) # Create a Quantum Circuit with 2 Qubits qc = QuantumCircuit(q) # Add a H gate on qubit 0, putting this qubit in superposition. qc.h(q[0]) # Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting # the qubits in a Bell state. qc.cx(q[0], q[1]) # See a list of available local simulators print("QCGPU backends: ", Provider.backends()) backend_sim = Provider.get_backend('statevector_simulator') # Compile and run the Quantum circuit on a simulator backend job_sim = execute(qc, backend_sim) result_sim = job_sim.result() # Show the results print("Simulation Results: ", result_sim)
# Quantum Fourier Transform for j in range(num_qubits): for k in range(j): circ.cu1(math.pi / float(2**(j - k)), q[j], q[k]) circ.h(q[j]) # circ.measure(q, c) return circ # Benchmarking functions # qiskit_backend = Aer.get_backend('qasm_simulator') # qcgpu_backend = QCGPUProvider().get_backend('qasm_simulator') qiskit_backend = Aer.get_backend('statevector_simulator') qcgpu_backend = QCGPUProvider().get_backend('statevector_simulator') def bench_qiskit(qc): start = time.time() job_sim = execute(qc, qiskit_backend) sim_result = job_sim.result() return time.time() - start def bench_qcgpu(qc): start = time.time() job_sim = execute(qc, qcgpu_backend) sim_result = job_sim.result() return time.time() - start
"""Usage examples for the QCGPU backends""" from qiskit_qcgpu_provider import QCGPUProvider Provider = QCGPUProvider() print(Provider.backends()) print(Provider.backends(name='statevector_simulator')) backend = Provider.get_backend('statevector_simulator') print(backend) # gets the name of the backend. print(backend.name()) # gets the status of the backend. print(backend.status()) # returns the provider of the backend print(backend.provider) # gets the configuration of the backend. print(backend.configuration()) # gets the properties of the backend. print(backend.properties())
from qiskit_qcgpu_provider import QCGPUProvider # Create a Quantum Register with 2 qubits. q = QuantumRegister(2) # Create a Classical Register with 2 bits. c = ClassicalRegister(2) # Create a Quantum Circuit qc = QuantumCircuit(q, c) # Add a H gate on qubit 0, putting this qubit in superposition. qc.h(q[0]) # Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting # the qubits in a Bell state. qc.cx(q[0], q[1]) # Add a Measure gate to see the state. qc.measure(q, c) # Get the QCGPU Provider Provider = QCGPUProvider() # See a list of available local simulators print("QCGPU backends: ", Provider.backends()) backend_sim = Provider.get_backend('qasm_simulator') # Compile and run the Quantum circuit on a simulator backend job_sim = execute(qc, backend_sim) result_sim = job_sim.result() # Show the results print(result_sim.get_counts(qc))