class QasmAlgorithmTestsWaltzBasis: """QasmSimulator algorithm tests in the Waltz u1,u2,u3,cx basis""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test algorithms # --------------------------------------------------------------------- def test_grovers_waltz_basis_gates(self): """Test grovers gate circuits compiling to u1,u2,u3,cx""" shots = 2000 circuits = ref_algorithms.grovers_circuit(final_measure=True, allow_sampling=True) targets = ref_algorithms.grovers_counts(shots) qobj = compile(circuits, self.SIMULATOR, shots=shots, basis_gates=['u1', 'u2', 'u3', 'cx']) result = self.SIMULATOR.run(qobj).result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) def test_teleport_waltz_basis_gates(self): """Test teleport gate circuits compiling to u1,u2,u3,cx""" shots = 2000 circuits = ref_algorithms.teleport_circuit() targets = ref_algorithms.teleport_counts(shots) qobj = compile(circuits, self.SIMULATOR, shots=shots, basis_gates=['u1', 'u2', 'u3', 'cx']) result = self.SIMULATOR.run(qobj).result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
def test_reset_nondeterministic(self): """Test ExtendedStabilizer reset with for circuits with non-deterministic counts""" # For statevector output we can combine deterministic and non-deterministic # count output circuits shots = 2000 circuits = ref_reset.reset_circuits_nondeterministic(final_measure=True) qobj = qiskit_compile(circuits, QasmSimulator(), shots=shots) targets = ref_reset.reset_counts_nondeterministic(shots) job = QasmSimulator().run(qobj, backend_options={ "method": "extended_stabilizer", "disable_measurement_opt": False }) result = job.result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
class QasmAlgorithmTests: """QasmSimulator algorithm tests in the default basis""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test algorithms # --------------------------------------------------------------------- def test_grovers_default_basis_gates(self): """Test grovers circuits compiling to backend default basis_gates.""" shots = 2000 circuits = ref_algorithms.grovers_circuit(final_measure=True, allow_sampling=True) targets = ref_algorithms.grovers_counts(shots) qobj = compile(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj).result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) def test_teleport_default_basis_gates(self): """Test teleport circuits compiling to backend default basis_gates.""" shots = 2000 circuits = ref_algorithms.teleport_circuit() targets = ref_algorithms.teleport_counts(shots) qobj = compile(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj).result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
class QasmConditionalKrausTests: """QasmSimulator conditional tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test conditional # --------------------------------------------------------------------- def test_conditional_unitary_1bit(self): """Test conditional kraus operations on 1-bit conditional register.""" shots = 100 circuits = ref_conditionals.conditional_circuits_1bit( final_measure=True, conditional_type='kraus') targets = ref_conditionals.conditional_counts_1bit(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS).result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_conditional_kraus_2bit(self): """Test conditional kraus operations on 2-bit conditional register.""" shots = 100 circuits = ref_conditionals.conditional_circuits_2bit( final_measure=True, conditional_type='kraus') targets = ref_conditionals.conditional_counts_2bit(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS).result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0)
class QasmUnitaryGateTests: """QasmSimulator additional tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test unitary gate qobj instruction # --------------------------------------------------------------------- def test_unitary_gate(self): """Test simulation with unitary gate circuit instructions.""" shots = 100 circuits = ref_unitary_gate.unitary_gate_circuits_deterministic( final_measure=True) targets = ref_unitary_gate.unitary_gate_counts_deterministic(shots) result = execute(circuits, self.SIMULATOR, shots=shots).result() self.assertTrue(getattr(result, 'success', False)) self.compare_counts(result, circuits, targets, delta=0) def test_random_unitary_gate(self): """Test simulation with random unitary gate circuit instructions.""" shots = 2000 circuits = ref_unitary_gate.unitary_random_gate_circuits_nondeterministic( final_measure=True) targets = ref_unitary_gate.unitary_random_gate_counts_nondeterministic( ) result = execute(circuits, self.SIMULATOR, shots=shots).result() self.assertTrue(getattr(result, 'success', False)) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
def __init__(self): self.timeout = 60 * 20 self.qv_circuits = [] self.backend = QasmSimulator() for num_qubits in (5, 10, 15): for depth in (10, ): # We want always the same seed, as we want always the same # circuits for the same value pairs of qubits and depth circ = quantum_volume_circuit(num_qubits, depth, seed=1) circ = transpile(circ, basis_gates=['u1', 'u2', 'u3', 'cx'], optimization_level=0, seed_transpiler=1) qobj = assemble(circ, self.backend, shots=1) self.qv_circuits.append(qobj) self.param_names = ["Quantum Volume", "Noise Model"] # This will run every benchmark for one of the combinations we have: # bench(qv_circuits, None) => bench(qv_circuits, mixed()) => # bench(qv_circuits, reset) => bench(qv_circuits, kraus()) self.params = (self.qv_circuits, [ no_noise(), mixed_unitary_noise_model(), reset_noise_model(), kraus_noise_model() ])
def test_parameterized_qobj_qasm_snapshot_expval(self): """Test parameterized qobj with Expectation Value snapshot and qasm simulator.""" shots = 1000 labels = snapshot_expval_labels() * 3 counts_targets = snapshot_expval_counts(shots) * 3 value_targets = snapshot_expval_pre_meas_values() * 3 backend = QasmSimulator() qobj = self.parameterized_qobj(backend=backend, shots=1000, measure=True, snapshot=True) self.assertIn('parameterizations', qobj.to_dict()['config']) job = backend.run(qobj, self.BACKEND_OPTS) result = job.result() success = getattr(result, 'success', False) num_circs = len(result.to_dict()['results']) self.assertTrue(success) self.compare_counts(result, range(num_circs), counts_targets, delta=0.1 * shots) # Check snapshots for j in range(num_circs): data = result.data(j) all_snapshots = self.expval_snapshots(data, labels) for label in labels: snaps = all_snapshots.get(label, {}) self.assertTrue(len(snaps), 1) for memory, value in snaps.items(): target = value_targets[j].get(label, {}).get(memory, {}) self.assertAlmostEqual(value, target, delta=1e-7)
class QasmExtraTests: """QasmSimulator additional tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test unitary gate qobj instruction # --------------------------------------------------------------------- def DISABLED_test_unitary_gate_real(self): """Test unitary qobj instruction with real matrices.""" shots = 100 qobj = ref_unitary_gate.unitary_gate_circuits_real_deterministic( final_measure=True) qobj.config.shots = shots circuits = [experiment.header.name for experiment in qobj.experiments] targets = ref_unitary_gate.unitary_gate_counts_real_deterministic( shots) result = self.SIMULATOR.run(qobj).result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0) def DISABLED_test_unitary_gate_complex(self): """Test unitary qobj instruction with complex matrices.""" shots = 100 qobj = ref_unitary_gate.unitary_gate_circuits_complex_deterministic( final_measure=True) qobj.config.shots = shots circuits = [experiment.header.name for experiment in qobj.experiments] targets = ref_unitary_gate.unitary_gate_counts_complex_deterministic( shots) result = self.SIMULATOR.run(qobj).result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0)
class QasmAlgorithmTestsMinimalBasis: """QasmSimulator algorithm tests in the minimal U,CX basis""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test algorithms # --------------------------------------------------------------------- def test_grovers_minimal_basis_gates(self): """Test grovers circuits compiling to u3,cx""" shots = 4000 circuits = ref_algorithms.grovers_circuit(final_measure=True, allow_sampling=True) targets = ref_algorithms.grovers_counts(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx']) result = job.result() self.assertTrue(getattr(result, 'success', False)) self.compare_counts(result, circuits, targets, delta=0.05 * shots) def test_teleport_minimal_basis_gates(self): """Test teleport gate circuits compiling to u3,cx""" shots = 4000 circuits = ref_algorithms.teleport_circuit() targets = ref_algorithms.teleport_counts(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx']) result = job.result() self.assertTrue(getattr(result, 'success', False)) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
class QasmConditionalTests(common.QiskitAerTestCase): """QasmSimulator conditional tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test conditional # --------------------------------------------------------------------- def test_conditional_1bit(self): """Test conditional operations on 1-bit conditional register.""" shots = 100 circuits = ref_conditionals.conditional_circuits_1bit( final_measure=True) targets = ref_conditionals.conditional_counts_1bit(shots) qobj = compile(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS).result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0) def test_conditional_2bit(self): """Test conditional operations on 2-bit conditional register.""" shots = 100 circuits = ref_conditionals.conditional_circuits_2bit( final_measure=True) targets = ref_conditionals.conditional_counts_2bit(shots) qobj = compile(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS).result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0)
class QasmMatrixProductStateMeasureTests: """QasmSimulator matrix_product_state measure tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test measure # --------------------------------------------------------------------- def test_measure_deterministic_with_sampling(self): """Test QasmSimulator measure with deterministic counts with sampling""" shots = 100 circuits = ref_measure.measure_circuits_deterministic( allow_sampling=True) targets = ref_measure.measure_counts_deterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS).result() self.assertTrue(getattr(result, 'success', False)) self.compare_counts(result, circuits, targets, delta=0) # Test sampling was enabled for res in result.results: self.assertIn("measure_sampling", res.metadata) self.assertEqual(res.metadata["measure_sampling"], True) def test_measure_nondeterministic_with_sampling(self): """Test QasmSimulator measure with non-deterministic counts with sampling""" shots = 2000 circuits = ref_measure.measure_circuits_nondeterministic( allow_sampling=True) targets = ref_measure.measure_counts_nondeterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS).result() self.assertTrue(getattr(result, 'success', False)) self.compare_counts(result, circuits, targets, delta=0.05 * shots) # Test sampling was enabled for res in result.results: self.assertIn("measure_sampling", res.metadata) self.assertEqual(res.metadata["measure_sampling"], True) # --------------------------------------------------------------------- # Test multi-qubit measure qobj instruction # --------------------------------------------------------------------- def test_measure_nondeterministic_multi_qubit_with_sampling(self): """Test QasmSimulator measure with non-deterministic counts""" shots = 2000 circuits = ref_measure.multiqubit_measure_circuits_nondeterministic( allow_sampling=True) targets = ref_measure.multiqubit_measure_counts_nondeterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS).result() self.assertTrue(getattr(result, 'success', False)) self.compare_counts(result, circuits, targets, delta=0.05 * shots) # Test sampling was enabled for res in result.results: self.assertIn("measure_sampling", res.metadata) self.assertEqual(res.metadata["measure_sampling"], True)
class QasmMultiplexerTests: """QasmSimulator multiplexer gate tests in default basis.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test multiplexer-cx-gate # --------------------------------------------------------------------- def test_multiplexer_cx_gate_deterministic(self): """Test multiplxer cx-gate circuits compiling to backend default basis_gates.""" shots = 100 circuits = ref_multiplexer.multiplexer_cx_gate_circuits_deterministic( final_measure=True) targets = ref_multiplexer.multiplexer_cx_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, backend_options=self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_multiplexer_cx_gate_nondeterministic(self): """Test multiplexer cx-gate circuits compiling to backend default basis_gates.""" shots = 4000 circuits = ref_multiplexer.multiplexer_cx_gate_circuits_nondeterministic( final_measure=True) targets = ref_multiplexer.multiplexer_cx_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, backend_options=self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) # --------------------------------------------------------------------- # Test multiplexer-gate # --------------------------------------------------------------------- def test_multiplexer_cxx_gate_deterministic(self): """Test multiplexer-gate gate circuits """ shots = 100 circuits = ref_multiplexer.multiplexer_ccx_gate_circuits_deterministic( final_measure=True) targets = ref_multiplexer.multiplexer_ccx_gate_counts_deterministic( shots) job = execute(circuits, self.SIMULATOR, shots=shots, backend_options=self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_multiplexer_cxx_gate_nondeterministic(self): """Test multiplexer ccx-gate gate circuits """ shots = 4000 circuits = ref_multiplexer.multiplexer_ccx_gate_circuits_nondeterministic( final_measure=True) targets = ref_multiplexer.multiplexer_ccx_gate_counts_nondeterministic( shots) job = execute(circuits, self.SIMULATOR, shots=shots, backend_options=self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
class TestQasmSimulatorStatevector(common.QiskitAerTestCase, StatevectorTests): """QasmSimulator statevector method tests.""" BACKEND_OPTS = { "seed_simulator": 271828, "method": "statevector", "max_parallel_threads": 1 } SIMULATOR = QasmSimulator(**BACKEND_OPTS)
def test_teleport_minimal_basis_gates(self): """Test teleport gate circuits compiling to U,CX""" shots = 2000 circuits = ref_algorithms.teleport_circuit() targets = ref_algorithms.teleport_counts(shots) job = execute(circuits, QasmSimulator(), shots=shots, basis_gates='U,CX') result = job.result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
def test_teleport_default_basis_gates(self): """Test teleport circuits compiling to backend default basis_gates.""" shots = 2000 circuits = ref_algorithms.teleport_circuit() targets = ref_algorithms.teleport_counts(shots) job = execute(circuits, QasmSimulator(), shots=shots) result = job.result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
def test_measure_nondeterministic_without_sampling(self): """Test QasmSimulator measure with nin-deterministic counts without sampling""" shots = 2000 circuits = ref_measure.measure_circuits_nondeterministic(allow_sampling=False) targets = ref_measure.measure_counts_nondeterministic(shots) job = execute(circuits, QasmSimulator(), shots=shots) result = job.result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
def test_ccx_gate_nondeterministic_minimal_basis_gates(self): """Test ccx-gate gate circuits compiling to U,CX""" shots = 2000 circuits = ref_non_clifford.ccx_gate_circuits_nondeterministic(final_measure=True) targets = ref_non_clifford.ccx_gate_counts_nondeterministic(shots) job = execute(circuits, QasmSimulator(), shots=shots, basis_gates='U,CX') result = job.result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
class QasmResetTests: """QasmSimulator reset tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test reset # --------------------------------------------------------------------- def test_reset_deterministic(self): """Test QasmSimulator reset with for circuits with deterministic counts""" # For statevector output we can combine deterministic and non-deterministic # count output circuits shots = 100 circuits = ref_reset.reset_circuits_deterministic(final_measure=True) targets = ref_reset.reset_counts_deterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS).result() self.assertTrue(getattr(result, 'success', False)) self.compare_counts(result, circuits, targets, delta=0) def test_reset_nondeterministic(self): """Test QasmSimulator reset with for circuits with non-deterministic counts""" # For statevector output we can combine deterministic and non-deterministic # count output circuits shots = 4000 circuits = ref_reset.reset_circuits_nondeterministic( final_measure=True) targets = ref_reset.reset_counts_nondeterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS).result() self.assertTrue(getattr(result, 'success', False)) self.compare_counts(result, circuits, targets, delta=0.05 * shots) def test_reset_sampling_opt(self): """Test sampling optimization""" shots = 4000 circuits = ref_reset.reset_circuits_sampling_optimization() targets = ref_reset.reset_counts_sampling_optimization(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS).result() self.assertTrue(getattr(result, 'success', False)) self.compare_counts(result, circuits, targets, delta=0.05 * shots) def test_repeated_resets(self): """Test repeated reset operations""" shots = 100 circuits = ref_reset.reset_circuits_repeated() targets = ref_reset.reset_counts_repeated(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS).result() self.assertTrue(getattr(result, 'success', False)) self.compare_counts(result, circuits, targets, delta=0)
def test_tdg_gate_nondeterministic_default_basis_gates(self): """Test tdg-gate circuits compiling to backend default basis_gates.""" shots = 2000 circuits = ref_non_clifford.tdg_gate_circuits_nondeterministic(final_measure=True) targets = ref_non_clifford.tdg_gate_counts_nondeterministic(shots) job = execute(circuits, QasmSimulator(), shots=shots) result = job.result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
def test_ccx_gate_deterministic_waltz_basis_gates(self): """Test ccx-gate gate circuits compiling to u1,u2,u3,cx""" shots = 100 circuits = ref_non_clifford.ccx_gate_circuits_deterministic(final_measure=True) targets = ref_non_clifford.ccx_gate_counts_deterministic(shots) job = execute(circuits, QasmSimulator(), shots=shots, basis_gates='u1,u2,u3,cx') result = job.result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0)
def test_measure_deterministic_with_sampling(self): """Test QasmSimulator measure with deterministic counts with sampling""" shots = 100 circuits = ref_measure.measure_circuits_deterministic(allow_sampling=True) targets = ref_measure.measure_counts_deterministic(shots) job = execute(circuits, QasmSimulator(), shots=shots) result = job.result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0)
def test_conditional_2bit(self): """Test conditional operations on 2-bit conditional register.""" shots = 100 circuits = ref_conditionals.conditional_circuits_2bit(final_measure=True) targets = ref_conditionals.conditional_counts_2bit(shots) job = execute(circuits, QasmSimulator(), shots=shots) result = job.result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0)
class TestQasmSimulatorDensityMatrix(common.QiskitAerTestCase, DensityMatrixTests): """QasmSimulator density_matrix method tests.""" BACKEND_OPTS = { "seed_simulator": 314159, "method": "density_matrix", "max_parallel_threads": 1 } SIMULATOR = QasmSimulator(**BACKEND_OPTS)
def hobo_qubo_mixer_measurement(hamildict, noise_model, angles, diag_vals, ps_end=False, mid_measure=None): n = int(np.max(hamildict[:, 1])) qr = QuantumRegister(n**2) main_circuit = QuantumCircuit(qr) times = len(angles) qubo_theta = angles[0:int(times / 2)] mixer_phi = angles[int(times / 2):] main_circuit += hadamards_position(n, qr) # Merging circuits for theta, phi in zip(qubo_theta, mixer_phi): main_circuit += hobo2qubo(n, qr) + qubo_circuit_simplified( hamildict, qr, theta) + hobo2qubo(n, qr).inverse() main_circuit += make_mixer(n, qr, phi) if mid_measure == 'no-ps': main_circuit += kraus_gates(qr, n, False) elif mid_measure == 'ps': main_circuit += kraus_gates(qr, n, True) else: pass if ps_end == 'end': main_circuit += kraus_gates(qr, n, True) main_circuit += hobo2qubo(n, qr) # Swap all for i in range(int(n**2 / 2)): main_circuit.swap(i, n**2 - 1 - i) #Preparation of Density-Matrix: backend = QasmSimulator(method='density_matrix', noise_model=noise_model, basis_gates=['cx', 'u1', 'u2', 'u3', 'kraus']) main_circuit.append(Snapshot('ss', 'density_matrix', n**2), qr) job = execute(main_circuit, backend=backend) result = job.result().results[0].to_dict( )['data']['snapshots']['density_matrix']['ss'][0]['value'] dm = np.asmatrix(result) #finds probability prob = np.trace(dm.real) #find mean energy mean_energy = np.real(np.sum(np.diag(dm) * diag_vals)) / prob max_energy = max(diag_vals) min_energy = min(diag_vals) energy = (mean_energy - min_energy) / (max_energy - min_energy) return energy, prob
class QasmPauliNoiseTests: """QasmSimulator pauli error noise model tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} def test_pauli_gate_noise(self): """Test simulation with Pauli gate error noise model.""" shots = 1000 circuits = ref_pauli_noise.pauli_gate_error_circuits() noise_models = ref_pauli_noise.pauli_gate_error_noise_models() targets = ref_pauli_noise.pauli_gate_error_counts(shots) for circuit, noise_model, target in zip(circuits, noise_models, targets): qobj = assemble(circuit, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, noise_model=noise_model, **self.BACKEND_OPTS).result() self.assertSuccess(result) self.compare_counts(result, [circuit], [target], delta=0.05 * shots) def test_pauli_reset_noise(self): """Test simulation with Pauli reset error noise model.""" shots = 1000 circuits = ref_pauli_noise.pauli_reset_error_circuits() noise_models = ref_pauli_noise.pauli_reset_error_noise_models() targets = ref_pauli_noise.pauli_reset_error_counts(shots) for circuit, noise_model, target in zip(circuits, noise_models, targets): qobj = assemble(circuit, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, noise_model=noise_model, **self.BACKEND_OPTS).result() self.assertSuccess(result) self.compare_counts(result, [circuit], [target], delta=0.05 * shots) def test_pauli_measure_noise(self): """Test simulation with Pauli measure error noise model.""" shots = 1000 circuits = ref_pauli_noise.pauli_measure_error_circuits() noise_models = ref_pauli_noise.pauli_measure_error_noise_models() targets = ref_pauli_noise.pauli_measure_error_counts(shots) for circuit, noise_model, target in zip(circuits, noise_models, targets): qobj = assemble(circuit, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, noise_model=noise_model, **self.BACKEND_OPTS).result() self.assertSuccess(result) self.compare_counts(result, [circuit], [target], delta=0.05 * shots)
class QasmSaveStateTests: """QasmSimulator SaveState instruction tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} def test_save_state(self): """Test save_amplitudes instruction""" REFERENCE_SAVE = { 'automatic': SaveStabilizer, 'stabilizer': SaveStabilizer, 'statevector': SaveStatevector, 'statevector_gpu': SaveStatevector, 'statevector_thrust': SaveStatevector, 'density_matrix': SaveDensityMatrix, 'density_matrix_gpu': SaveDensityMatrix, 'density_matrix_thrust': SaveDensityMatrix } REFERENCE_LABEL = { 'automatic': 'stabilizer', 'stabilizer': 'stabilizer', 'statevector': 'statevector', 'statevector_gpu': 'statevector', 'statevector_thrust': 'statevector', 'density_matrix': 'density_matrix', 'density_matrix_gpu': 'density_matrix', 'density_matrix_thrust': 'density_matrix' } opts = self.BACKEND_OPTS.copy() method = opts.get('method', 'automatic') if method in REFERENCE_SAVE: # Stabilizer test circuit num_qubits = 4 target_instr = REFERENCE_SAVE[method](num_qubits, label='target') circ = QuantumCircuit(num_qubits) circ.h(0) for i in range(1, num_qubits): circ.cx(i - 1, i) circ.save_state() circ.append(target_instr, range(num_qubits)) label = REFERENCE_LABEL[method] # Run qobj = assemble(circ, self.SIMULATOR) result = self.SIMULATOR.run(qobj, **opts).result() self.assertTrue(result.success) data = result.data(0) self.assertIn(label, data) self.assertIn('target', data) value = data[label] target = data['target'] self.assertTrue(np.all(value == target))
class QasmStandardGateDensityMatrixTests: """QasmSimulator standard gate library tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} SEED = 9997 RNG = default_rng(seed=SEED) @data(*[(gate_params[0], gate_params[1], gate_params[2], basis_gates) for gate_params, basis_gates in product(GATES, BASIS_GATES)]) @unpack def test_gate_density_matrix(self, gate_cls, num_angles, has_ctrl_qubits, basis_gates): """Test standard gate simulation test.""" SUPPORTED_METHODS = [ 'automatic', 'statevector', 'statevector_gpu', 'statevector_thrust', 'density_matrix', 'density_matrix_gpu', 'density_matrix_thrust' ] backend_options = self.BACKEND_OPTS.copy() method = backend_options.pop('method', 'automatic') backend = self.SIMULATOR backend.set_options(method=method) circuits = self.gate_circuits(gate_cls, num_angles=num_angles, has_ctrl_qubits=has_ctrl_qubits, rng=self.RNG) for circuit in circuits: target = Statevector.from_instruction(circuit) # Add snapshot and execute circuit.snapshot_density_matrix('final') result = execute(circuit, backend, shots=1, basis_gates=basis_gates, optimization_level=0, **backend_options).result() # Check results success = getattr(result, 'success', False) msg = '{}, method = {}'.format(gate_cls.__name__, method) if method not in SUPPORTED_METHODS: self.assertFalse(success) else: self.assertTrue(success, msg=msg) self.assertSuccess(result) snapshots = result.data(0).get("snapshots", {}).get("density_matrix", {}) value = snapshots.get('final', [{'value': None}])[0]['value'] fidelity = state_fidelity(target, value) self.assertGreater(fidelity, 0.99999, msg=msg)
def set_qasm(sim_opts): sim_method = [opt for opt in sim_opts if opt.startswith('method')] if len(sim_method) == 0: backend = QasmSimulator(method='automatic', max_memory_mb=2048) sim_method = 'automatic' else: sim_method = sim_method[0].split('=')[1].strip('\n') if sim_method not in qasm_methods: print('SimulationError: simulation method not available. Available options: "statevector", "density_matrix", "matrix_product_state", "automatic"') sys.exit() else: backend = QasmSimulator(method=sim_method, max_memory_mb=2048) return backend, sim_method
def test_grovers_minimal_basis_gates(self): """Test grovers circuits compiling to U,CX""" shots = 2000 circuits = ref_algorithms.grovers_circuit(final_measure=True, allow_sampling=True) targets = ref_algorithms.grovers_counts(shots) job = execute(circuits, QasmSimulator(), shots=shots, basis_gates='U,CX') result = job.result() self.is_completed(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
def __init__(self,ising,p,qubits,useNoiseModel = False): self.ising = ising self.p = p self.useNoiseModel = useNoiseModel self.qubits = qubits self.register = None self.q = QuantumRegister(qubits) self.c = ClassicalRegister(qubits) self.register = QuantumCircuit(self.q, self.c) self.backend = QasmSimulator()