def qft(): print('start') state = qcgpu.State(24) num_qubits = state.num_qubits for j in range(num_qubits): for k in range(j): state.cu1(j, k, math.pi / float(2**(j - k))) state.h(j)
def bench_qcgpu(num_qubits): start = time.time() state = qcgpu.State(num_qubits) for j in range(num_qubits): for k in range(j): state.cu1(j, k, math.pi/float(2**(j-k))) state.h(j) state.backend.queue.finish() return time.time() - start
def bell_state(): import qcgpu print("Creating Bell State") state = qcgpu.State(2) state.h(0) state.cx(0, 1) print("Measurement Results:") print(state.measure(samples=1000))
def bench_qcgpu(n, depth): h = qcgpu.gate.h() x = qcgpu.gate.x() t = qcgpu.gate.t() state = qcgpu.State(n) start = time.time() for level in range(depth): for q in range(n): state.apply_gate(h, q) state.apply_gate(t, q) if q != 0: state.apply_controlled_gate(x, q, 0) return time.time() - start
def bernstein_vazirani(): import qcgpu num_qubits = 7 # The number of qubits to use a = 101 # The hidden integer, bitstring is 1100101 register = qcgpu.State(num_qubits) # Create a new quantum register register.apply_all(qcgpu.gate.h()) # Apply a hadamard gate to each qubit # Apply the inner products oracle for i in range(num_qubits): if a & (1 << i) != 0: register.z(i) register.apply_all(qcgpu.gate.h()) # Apply a hadamard gate to each qubit results = register.measure( samples=1000) # Measure the register (sample 1000 times) print(results)
def bench_qcgpu(n, depth): state = qcgpu.State(n) h = qcgpu.gate.h() x = qcgpu.gate.x() sqrt_x = qcgpu.gate.sqrt_x() start = time.time() for level in range(depth): for q in range(n): state.apply_gate(h, q) state.apply_gate(sqrt_x, q) if q != 0: state.apply_controlled_gate(x, q, 0) runtime = time.time() - start return {'name': 'qcgpu', 'num_qubits': n, 'depth': depth, 'time': runtime}
def bench(num_qubits, depth): start = time.time() state = qcgpu.State(num_qubits) rand_circuit(num_qubits, depth, state) state.backend.queue.finish() return time.time() - start
import qcgpu # 3 qubits, f(x) = x_0 NOT x_1 x_2 # Balanced balanced_state = qcgpu.State(3) balanced_state.apply_all(qcgpu.gate.h()) # Oracle U_f balanced_state.h(2) balanced_state.z(0) balanced_state.cx(1, 2) balanced_state.h(2) balanced_state.apply_all(qcgpu.gate.h()) outcomes = balanced_state.measure(samples=1000) if int(max(outcomes, key=outcomes.get)) == 0: print('constant') else: print('balanced') # 3 qubits, f(x) = 0 # Constant constant_state = qcgpu.State(3) constant_state.apply_all(qcgpu.gate.h()) # Oracle is equivalent to the identity gate, # thus has no effect on the state
import qcgpu import time s = qcgpu.State(28) h = qcgpu.gate.h() s.apply_all(h) print(s.measure(1000)) # print(s.backend.measure(samples=10000))
def run_experiment(self, experiment): """Run an experiment (circuit) and return a single experiment result. Args: experiment (QobjExperiment): experiment from qobj experiments list Returns: dict: A dictionary of results. dict: A result dictionary Raises: QCGPUSimulatorError: If the number of qubits is too large, or another error occurs during execution. """ self._number_of_qubits = experiment.header.n_qubits self._statevector = 0 start = time.time() try: sim = qcgpu.State(self._number_of_qubits) except OverflowError: raise QCGPUSimulatorError('too many qubits') for operation in experiment.instructions: name = operation.name qubits = operation.qubits params = [ float(param) for param in getattr(operation, 'params', []) ] if name == 'id': logger.info('Identity gates are ignored.') elif name == 'barrier': logger.info('Barrier gates are ignored.') elif name == 'u3': sim.u(qubits[0], *params) elif name == 'u2': sim.u2(qubits[0], *params) elif name == 'u1': sim.u1(qubits[0], *params) elif name == 'cx': sim.cx(*qubits) elif name == 'h': sim.h(qubits[0]) elif name == 'x': sim.x(qubits[0]) elif name == 'y': sim.y(qubits[0]) elif name == 'z': sim.z(qubits[0]) elif name == 's': sim.s(qubits[0]) elif name == 't': sim.t(qubits[0]) amps = [ complex(z) for z in sim.amplitudes().round(self._chop_threshold) ] end = time.time() # amps = np.stack((amps.real, amps.imag), axis=-1) return ExperimentResult(shots=1, success=True, data=ExperimentResultData(statevector=amps), time_taken=(end - start))
def run_experiment(self, experiment): """Run an experiment (circuit) and return a single experiment result. Args: experiment (QobjExperiment): experiment from qobj experiments list Returns: dict: A dictionary of results. dict: A result dictionary Raises: QCGPUSimulatorError: If the number of qubits is too large, or another error occurs during execution. """ self._number_of_qubits = experiment.header.n_qubits self._number_of_cbits = experiment.header.memory_slots self._classical_state = 0 self._statevector = 0 if hasattr(experiment.config, 'seed'): seed = experiment.config.seed elif hasattr(self._qobj_config, 'seed'): seed = self._qobj_config.seed else: # For compatibility on Windows force dyte to be int32 # and set the maximum value to be (2 ** 31) - 1 seed = np.random.randint(2147483647, dtype='int32') self._local_random.seed(seed) self._can_sample(experiment) if not self._sample_measure: raise QCGPUSimulatorError( 'Measurements are only supported at the end') experiment = experiment.to_dict() # qcgpu.backend.create_context() samples = [] start = time.time() try: sim = qcgpu.State(self._number_of_qubits) except OverflowError: raise QCGPUSimulatorError('too many qubits') for operation in experiment['instructions']: params = operation.get('params', []) name = operation['name'] if name == 'id': logger.info('Identity gates are ignored.') elif name == 'barrier': logger.info('Barrier gates are ignored.') elif name == 'u3': sim.u(operation['qubits'][0], *params) elif name == 'u2': sim.u2(operation['qubits'][0], *params) elif name == 'u1': sim.u1(operation['qubits'][0], *params) elif name == 'cx': sim.cx(*operation['qubits']) elif name == 'h': sim.h(operation['qubits'][0]) elif name == 'x': sim.x(operation['qubits'][0]) elif name == 'y': sim.y(operation['qubits'][0]) elif name == 'z': sim.z(operation['qubits'][0]) elif name == 's': sim.s(operation['qubits'][0]) elif name == 't': sim.t(operation['qubits'][0]) elif name == 'measure': samples.append( (operation['qubits'][0], operation['memory'][0])) if self._number_of_cbits > 0: memory = self._add_sample_measure(samples, sim, self._shots) else: memory = [] end = time.time() # amps = sim.amplitudes().round(self._chop_threshold) # amps = np.stack((amps.real, amps.imag), axis=-1) data = {'counts': dict(Counter(memory))} if self._memory: data['memory'] = memory return { 'name': experiment['header']['name'], 'shots': self._shots, 'data': data, 'seed': seed, 'status': 'DONE', 'success': True, 'time_taken': (end - start), 'header': experiment['header'] }
def run_experiment(self, experiment): """Run an experiment (circuit) and return a single experiment result. Args: experiment (QobjExperiment): experiment from qobj experiments list Returns: dict: A dictionary of results. dict: A result dictionary Raises: QCGPUSimulatorError: If the number of qubits is too large, or another error occurs during execution. """ self._number_of_qubits = experiment.header.n_qubits self._statevector = 0 experiment = experiment.as_dict() start = time.time() try: sim = qcgpu.State(self._number_of_qubits) except OverflowError: raise QCGPUSimulatorError('too many qubits') for operation in experiment['instructions']: params = operation['params'] name = operation['name'] if name == 'id': logger.info('Identity gates are ignored.') elif name == 'barrier': logger.info('Barrier gates are ignored.') elif name == 'u3': sim.u(operation['qubits'][0], *params) elif name == 'u2': sim.u2(operation['qubits'][0], *params) elif name == 'u1': sim.u1(operation['qubits'][0], *params) elif name == 'cx': sim.cx(*operation['qubits']) elif name == 'h': sim.h(operation['qubits'][0]) elif name == 'x': sim.x(operation['qubits'][0]) elif name == 'y': sim.y(operation['qubits'][0]) elif name == 'z': sim.z(operation['qubits'][0]) elif name == 's': sim.s(operation['qubits'][0]) elif name == 't': sim.t(operation['qubits'][0]) end = time.time() amps = sim.amplitudes().round(self._chop_threshold) amps = np.stack((amps.real, amps.imag), axis=-1) return { 'name': experiment['header']['name'], 'shots': 1, 'data': { 'statevector': amps }, 'status': 'DONE', 'success': True, 'time_taken': (end - start), 'header': experiment['header'] }