def test_teleport_default_basis_gates(self): """Test teleport circuits compiling to backend default basis_gates.""" shots = 1000 circuits = ref_algorithms.teleport_circuit() qobj = assemble(circuits, QasmSimulator(), shots=shots) targets = ref_algorithms.teleport_counts(shots) job = QasmSimulator().run(qobj, **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
def test_sdg_gate_nondeterministic_default_basis_gates(self): shots = 4000 """Test sdg-gate circuits compiling to backend default basis_gates.""" circuits = ref_1q_clifford.sdg_gate_circuits_nondeterministic( final_measure=True) qobj = assemble(circuits, QasmSimulator(), shots=shots) targets = ref_1q_clifford.sdg_gate_counts_nondeterministic(shots) job = QasmSimulator().run(qobj, **self.BACKEND_OPTS_SAMPLING) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
def test_t_gate_deterministic_default_basis_gates(self): """Test t-gate circuits compiling to backend default basis_gates.""" shots = 100 circuits = ref_non_clifford.t_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, QasmSimulator(), shots=shots) targets = ref_non_clifford.t_gate_counts_deterministic(shots) job = QasmSimulator().run(qobj, **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.1 * shots)
def test_conditional_2bit(self): """Test conditional operations on 2-bit conditional register.""" shots = 100 circuits = ref_conditionals.conditional_circuits_2bit( final_measure=True) qobj = assemble(circuits, QasmSimulator(), shots=shots) targets = ref_conditionals.conditional_counts_2bit(shots) job = QasmSimulator().run(qobj, **self.BACKEND_OPTS_SAMPLING) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0)
def test_measure_nondeterministic_multi_qubit_without_sampling(self): """Test CHimulator reset with non-deterministic counts""" shots = 4000 circuits = ref_measure.multiqubit_measure_circuits_nondeterministic( allow_sampling=False) targets = ref_measure.multiqubit_measure_counts_nondeterministic(shots) qobj = assemble(circuits, QasmSimulator(), shots=shots) job = QasmSimulator().run(qobj, **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
def test_measure_nondeterministic_with_sampling(self): """Test CHimulator measure with non-deterministic counts with sampling""" shots = 4000 circuits = ref_measure.measure_circuits_nondeterministic( allow_sampling=True) qobj = assemble(circuits, QasmSimulator(), shots=shots) targets = ref_measure.measure_counts_nondeterministic(shots) job = QasmSimulator().run(qobj, **self.BACKEND_OPTS_SAMPLING) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
def test_measure_deterministic_without_sampling(self): """Test ExtendedStabilizer measure with deterministic counts without sampling""" shots = 100 circuits = ref_measure.measure_circuits_deterministic( allow_sampling=False) qobj = assemble(circuits, QasmSimulator(), shots=shots) targets = ref_measure.measure_counts_deterministic(shots) job = QasmSimulator().run(qobj, **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0)
def test_reset_deterministic(self): """Test ExtendedStabilizer 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) qobj = assemble(circuits, QasmSimulator(), shots=shots) targets = ref_reset.reset_counts_deterministic(shots) job = QasmSimulator().run(qobj, **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0)
def test_grovers_default_basis_gates(self): """Test grovers circuits compiling to backend default basis_gates.""" shots = 500 circuits = ref_algorithms.grovers_circuit(final_measure=True, allow_sampling=True) qobj = assemble(circuits, QasmSimulator(), shots=shots) targets = ref_algorithms.grovers_counts(shots) opts = self.BACKEND_OPTS.copy() opts["extended_stabilizer_mixing_time"] = 100 job = QasmSimulator().run(qobj, **opts) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.1 * shots)
def test_ccx_gate_nondeterministic_default_basis_gates(self): """Test ccx-gate circuits compiling to backend default basis_gates.""" shots = 500 circuits = ref_non_clifford.ccx_gate_circuits_nondeterministic( final_measure=True) qobj = assemble(circuits, QasmSimulator(), shots=shots) targets = ref_non_clifford.ccx_gate_counts_nondeterministic(shots) opts = self.BACKEND_OPTS.copy() opts["extended_stabilizer_mixing_time"] = 100 job = QasmSimulator().run(qobj, **opts) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.10 * shots)
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'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) 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'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
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, **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, **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, **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, **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
class QasmDiagonalGateTests: """QasmSimulator diagonal gate tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test unitary gate qobj instruction # --------------------------------------------------------------------- def test_diagonal_gate(self): """Test simulation with unitary gate circuit instructions.""" shots = 100 circuits = ref_diagonal_gate.diagonal_gate_circuits_deterministic( final_measure=True) targets = ref_diagonal_gate.diagonal_gate_counts_deterministic(shots) result = execute(circuits, self.SIMULATOR, shots=shots, **self.BACKEND_OPTS).result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0)
class QasmStandardGateStatevectorTests: """QasmSimulator standard gate library tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} SEED = 8181 RNG = default_rng(seed=SEED) @data(*GATES) @unpack def test_gate_statevector(self, gate_cls, num_params): """Test standard gate simulation test.""" SUPPORTED_METHODS = [ 'automatic', 'statevector', 'statevector_gpu', 'statevector_thrust', 'matrix_product_state' ] circuit = self.gate_circuit(gate_cls, num_params=num_params, rng=self.RNG) target = Statevector.from_instruction(circuit) # Add snapshot and execute #circuit.snapshot_statevector('final') backend_options = self.BACKEND_OPTS method = backend_options.pop('method', 'automatic') backend = self.SIMULATOR backend.set_options(method=method) result = execute(circuit, backend, shots=1, **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)
class QasmNonCliffordTestsCCXGate: """QasmSimulator CCX gate tests in default basis.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test ccx-gate # --------------------------------------------------------------------- def test_ccx_gate_deterministic_default_basis_gates(self): """Test ccx-gate circuits compiling to backend default basis_gates.""" 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, self.SIMULATOR, shots=shots, **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_ccx_gate_nondeterministic_default_basis_gates(self): """Test ccx-gate circuits compiling to backend default basis_gates.""" shots = 4000 circuits = ref_non_clifford.ccx_gate_circuits_nondeterministic( final_measure=True) targets = ref_non_clifford.ccx_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
class QasmNonCliffordTestsMinimalBasis: """QasmSimulator non-Clifford gate tests in minimal U,CX basis.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test t-gate # --------------------------------------------------------------------- def test_t_gate_deterministic_minimal_basis_gates(self): """Test t-gate gate circuits compiling to u3,cx""" shots = 100 circuits = ref_non_clifford.t_gate_circuits_deterministic( final_measure=True) targets = ref_non_clifford.t_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx']) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_t_gate_nondeterministic_minimal_basis_gates(self): """Test t-gate gate circuits compiling to u3,cx""" shots = 4000 circuits = ref_non_clifford.t_gate_circuits_nondeterministic( final_measure=True) targets = ref_non_clifford.t_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) # --------------------------------------------------------------------- # Test tdg-gate # --------------------------------------------------------------------- def test_tdg_gate_deterministic_minimal_basis_gates(self): """Test tdg-gate gate circuits compiling to u3,cx""" shots = 100 circuits = ref_non_clifford.tdg_gate_circuits_deterministic( final_measure=True) targets = ref_non_clifford.tdg_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_tdg_gate_nondeterministic_minimal_basis_gates(self): """Test tdg-gate gate circuits compiling to u3,cx""" shots = 4000 circuits = ref_non_clifford.tdg_gate_circuits_nondeterministic( final_measure=True) targets = ref_non_clifford.tdg_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) # --------------------------------------------------------------------- # Test ccx-gate # --------------------------------------------------------------------- def test_ccx_gate_deterministic_minimal_basis_gates(self): """Test ccx-gate gate circuits compiling to 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, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_ccx_gate_nondeterministic_minimal_basis_gates(self): """Test ccx-gate gate circuits compiling to u3,cx""" shots = 4000 circuits = ref_non_clifford.ccx_gate_circuits_nondeterministic( final_measure=True) targets = ref_non_clifford.ccx_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.1 * shots) # --------------------------------------------------------------------- # Test cu1 gate # --------------------------------------------------------------------- def test_cu1_gate_nondeterministic_minimal_basis_gates(self): """Test cu1-gate gate circuits compiling to u3,cx""" shots = 4000 circuits = ref_non_clifford.cu1_gate_circuits_nondeterministic( final_measure=True) targets = ref_non_clifford.cu1_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.1 * shots) # --------------------------------------------------------------------- # Test cswap-gate (Fredkin) # --------------------------------------------------------------------- def test_cswap_gate_deterministic_minimal_basis_gates(self): """Test cswap-gate gate circuits compiling to u3,cx""" shots = 100 circuits = ref_non_clifford.cswap_gate_circuits_deterministic( final_measure=True) targets = ref_non_clifford.cswap_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_cswap_gate_nondeterministic_minimal_basis_gates(self): """Test cswap-gate gate circuits compiling to u3,cx""" shots = 4000 circuits = ref_non_clifford.cswap_gate_circuits_nondeterministic( final_measure=True) targets = ref_non_clifford.cswap_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.1 * shots) # --------------------------------------------------------------------- # Test cu3 gate # --------------------------------------------------------------------- def test_cu3_gate_deterministic_default_basis_gates(self): """Test cu3-gate gate circuits compiling to u3, cx.""" shots = 100 circuits = ref_non_clifford.cu3_gate_circuits_deterministic( final_measure=True) targets = ref_non_clifford.cu3_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0)
class QasmNonCliffordTestsCGates: """QasmSimulator CSwap gate tests in default basis.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test cswap-gate (Fredkin) # --------------------------------------------------------------------- def test_cswap_gate_deterministic_default_basis_gates(self): shots = 100 circuits = ref_non_clifford.cswap_gate_circuits_deterministic( final_measure=True) targets = ref_non_clifford.cswap_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_cswap_gate_nondeterministic_default_basis_gates(self): shots = 4000 circuits = ref_non_clifford.cswap_gate_circuits_nondeterministic( final_measure=True) targets = ref_non_clifford.cswap_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) # --------------------------------------------------------------------- # Test cu1 gate # --------------------------------------------------------------------- def test_cu1_gate_nondeterministic_default_basis_gates(self): """Test cu1-gate gate circuits compiling to default basis.""" shots = 4000 circuits = ref_non_clifford.cu1_gate_circuits_nondeterministic( final_measure=True) targets = ref_non_clifford.cu1_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) # --------------------------------------------------------------------- # Test cu3 gate # --------------------------------------------------------------------- def test_cu3_gate_deterministic_default_basis_gates(self): """Test cu3-gate gate circuits compiling to default basis.""" shots = 100 circuits = ref_non_clifford.cu3_gate_circuits_deterministic( final_measure=True) targets = ref_non_clifford.cu3_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0)
class QasmInitializeTests: """QasmSimulator initialize tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test initialize instr make it through the wrapper # --------------------------------------------------------------------- def test_initialize_wrapper_1(self): """Test QasmSimulator initialize""" shots = 100 lst = [0, 1] init_states = [ np.array(lst), np.array(lst, dtype=float), np.array(lst, dtype=np.float32), np.array(lst, dtype=complex), np.array(lst, dtype=np.complex64) ] circuits = [] [ circuits.extend(ref_initialize.initialize_circuits_w_1(init_state)) for init_state in init_states ] qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, **self.BACKEND_OPTS).result() self.assertSuccess(result) # --------------------------------------------------------------------- # Test initialize instr make it through the wrapper # --------------------------------------------------------------------- def test_initialize_wrapper_2(self): """Test QasmSimulator initialize""" shots = 100 lst = [0, 1, 0, 0] init_states = [ np.array(lst), np.array(lst, dtype=float), np.array(lst, dtype=np.float32), np.array(lst, dtype=complex), np.array(lst, dtype=np.complex64) ] circuits = [] [ circuits.extend(ref_initialize.initialize_circuits_w_2(init_state)) for init_state in init_states ] qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, **self.BACKEND_OPTS).result() self.assertSuccess(result) # --------------------------------------------------------------------- # Test initialize # --------------------------------------------------------------------- def test_initialize_1(self): """Test QasmSimulator initialize""" # For statevector output we can combine deterministic and non-deterministic # count output circuits shots = 1000 circuits = ref_initialize.initialize_circuits_1(final_measure=True) targets = ref_initialize.initialize_counts_1(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) opts = self.BACKEND_OPTS.copy() result = self.SIMULATOR.run(qobj, **opts).result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) def test_initialize_2(self): """Test QasmSimulator initializes""" # For statevector output we can combine deterministic and non-deterministic # count output circuits shots = 1000 circuits = ref_initialize.initialize_circuits_2(final_measure=True) targets = ref_initialize.initialize_counts_2(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) opts = self.BACKEND_OPTS.copy() result = self.SIMULATOR.run(qobj, **opts).result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) def test_initialize_sampling_opt(self): """Test sampling optimization""" shots = 1000 circuits = ref_initialize.initialize_sampling_optimization() targets = ref_initialize.initialize_counts_sampling_optimization(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) opts = self.BACKEND_OPTS.copy() result = self.SIMULATOR.run(qobj, **opts).result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
class QasmQubitsTruncateTests: """QasmSimulator Qubits Truncate tests.""" SIMULATOR = QasmSimulator() def create_circuit_for_truncate(self): qr = QuantumRegister(4) cr = ClassicalRegister(4) circuit = QuantumCircuit(qr, cr) circuit.u3(0.1,0.1,0.1,qr[1]) circuit.barrier(qr) circuit.x(qr[2]) circuit.barrier(qr) circuit.x(qr[1]) circuit.barrier(qr) circuit.x(qr[3]) circuit.barrier(qr) circuit.u3(0.1,0.1,0.1,qr[0]) circuit.barrier(qr) circuit.measure(qr[0], cr[0]) circuit.measure(qr[1], cr[1]) return circuit def device_properties(self): properties = {"general": [], "last_update_date": "2019-04-22T03:26:08+00:00", "gates": [ {"gate": "u1", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [0]}, {"gate": "u2", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [0]}, {"gate": "u3", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [0]}, {"gate": "u1", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [1]}, {"gate": "u2", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [1]}, {"gate": "u3", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [1]}, {"gate": "u1", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [2]}, {"gate": "u2", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [2]}, {"gate": "u3", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [2]}, {"gate": "u1", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [3]}, {"gate": "u2", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [3]}, {"gate": "u3", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [3]}, {"gate": "u1", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [4]}, {"gate": "u2", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [4]}, {"gate": "u3", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [4]}, {"gate": "u1", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [5]}, {"gate": "u2", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [5]}, {"gate": "u3", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [5]}, {"gate": "u1", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [6]}, {"gate": "u2", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [6]}, {"gate": "u3", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [6]}, {"gate": "u1", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [7]}, {"gate": "u2", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [7]}, {"gate": "u3", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [7]}, {"gate": "u1", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [8]}, {"gate": "u2", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [8]}, {"gate": "u3", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [8]}, {"gate": "u1", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [9]}, {"gate": "u2", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [9]}, {"gate": "u3", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-23T01:45:04+00:00", "unit": ""}], "qubits": [9]}, {"gate": "cx", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-22T02:26:00+00:00", "unit": ""}], "qubits": [0, 1], "name": "CX0_1"}, {"gate": "cx", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-22T02:29:15+00:00", "unit": ""}], "qubits": [1, 2], "name": "CX1_2"}, {"gate": "cx", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-22T02:32:48+00:00", "unit": ""}], "qubits": [2, 3], "name": "CX2_3"}, {"gate": "cx", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-22T02:26:00+00:00", "unit": ""}], "qubits": [3, 4], "name": "CX3_4"}, {"gate": "cx", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-22T02:29:15+00:00", "unit": ""}], "qubits": [4, 5], "name": "CX4_5"}, {"gate": "cx", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-22T02:32:48+00:00", "unit": ""}], "qubits": [5, 6], "name": "CX5_6"}, {"gate": "cx", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-22T02:26:00+00:00", "unit": ""}], "qubits": [6, 7], "name": "CX6_7"}, {"gate": "cx", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-22T02:29:15+00:00", "unit": ""}], "qubits": [7, 8], "name": "CX7_8"}, {"gate": "cx", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-22T02:32:48+00:00", "unit": ""}], "qubits": [8, 9], "name": "CX8_9"}, {"gate": "cx", "parameters": [{"name": "gate_error", "value": 0.001, "date": "2019-04-22T02:26:00+00:00", "unit": ""}], "qubits": [9, 0], "name": "CX9_0"}], "qubits": [ [ {"name": "T1", "value": 23.809868955712616, "date": "2019-04-22T01:30:15+00:00", "unit": "\u00b5s"}, {"name": "T2", "value": 43.41142418044261, "date": "2019-04-22T01:33:33+00:00", "unit": "\u00b5s"}, {"name": "frequency", "value": 5.032871440179164, "date": "2019-04-22T03:26:08+00:00", "unit": "GHz"}, {"name": "readout_error", "value": 0.03489999999999993, "date": "2019-04-22T01:29:47+00:00", "unit": ""}], [ {"name": "T1", "value": 68.14048367144501, "date": "2019-04-22T01:30:15+00:00", "unit": "\u00b5s"}, {"name": "T2", "value": 56.95903203933663, "date": "2019-04-22T01:34:36+00:00", "unit": "\u00b5s"}, {"name": "frequency", "value": 4.896209948700639, "date": "2019-04-22T03:26:08+00:00", "unit": "GHz"}, {"name": "readout_error", "value": 0.19589999999999996, "date": "2019-04-22T01:29:47+00:00", "unit": ""}], [ {"name": "T1", "value": 83.26776276928099, "date": "2019-04-22T01:30:15+00:00", "unit": "\u00b5s"}, {"name": "T2", "value": 23.49615795695734, "date": "2019-04-22T01:31:32+00:00", "unit": "\u00b5s"}, {"name": "frequency", "value": 5.100093544085939, "date": "2019-04-22T03:26:08+00:00", "unit": "GHz"}, {"name": "readout_error", "value": 0.09050000000000002, "date": "2019-04-22T01:29:47+00:00", "unit": ""}], [ {"name": "T1", "value": 57.397746445609975, "date": "2019-04-22T01:30:15+00:00", "unit": "\u00b5s"}, {"name": "T2", "value": 98.47976889309517, "date": "2019-04-22T01:32:32+00:00", "unit": "\u00b5s"}, {"name": "frequency", "value": 5.238526396839902, "date": "2019-04-22T03:26:08+00:00", "unit": "GHz"}, {"name": "readout_error", "value": 0.24350000000000005, "date": "2019-04-20T15:31:39+00:00", "unit": ""}], [ {"name": "T1", "value": 23.809868955712616, "date": "2019-04-22T01:30:15+00:00", "unit": "\u00b5s"}, {"name": "T2", "value": 43.41142418044261, "date": "2019-04-22T01:33:33+00:00", "unit": "\u00b5s"}, {"name": "frequency", "value": 5.032871440179164, "date": "2019-04-22T03:26:08+00:00", "unit": "GHz"}, {"name": "readout_error", "value": 0.03489999999999993, "date": "2019-04-22T01:29:47+00:00", "unit": ""}], [ {"name": "T1", "value": 68.14048367144501, "date": "2019-04-22T01:30:15+00:00", "unit": "\u00b5s"}, {"name": "T2", "value": 56.95903203933663, "date": "2019-04-22T01:34:36+00:00", "unit": "\u00b5s"}, {"name": "frequency", "value": 4.896209948700639, "date": "2019-04-22T03:26:08+00:00", "unit": "GHz"}, {"name": "readout_error", "value": 0.19589999999999996, "date": "2019-04-22T01:29:47+00:00", "unit": ""}], [ {"name": "T1", "value": 83.26776276928099, "date": "2019-04-22T01:30:15+00:00", "unit": "\u00b5s"}, {"name": "T2", "value": 23.49615795695734, "date": "2019-04-22T01:31:32+00:00", "unit": "\u00b5s"}, {"name": "frequency", "value": 5.100093544085939, "date": "2019-04-22T03:26:08+00:00", "unit": "GHz"}, {"name": "readout_error", "value": 0.09050000000000002, "date": "2019-04-22T01:29:47+00:00", "unit": ""}], [ {"name": "T1", "value": 57.397746445609975, "date": "2019-04-22T01:30:15+00:00", "unit": "\u00b5s"}, {"name": "T2", "value": 98.47976889309517, "date": "2019-04-22T01:32:32+00:00", "unit": "\u00b5s"}, {"name": "frequency", "value": 5.238526396839902, "date": "2019-04-22T03:26:08+00:00", "unit": "GHz"}, {"name": "readout_error", "value": 0.24350000000000005, "date": "2019-04-20T15:31:39+00:00", "unit": ""}], [ {"name": "T1", "value": 23.809868955712616, "date": "2019-04-22T01:30:15+00:00", "unit": "\u00b5s"}, {"name": "T2", "value": 43.41142418044261, "date": "2019-04-22T01:33:33+00:00", "unit": "\u00b5s"}, {"name": "frequency", "value": 5.032871440179164, "date": "2019-04-22T03:26:08+00:00", "unit": "GHz"}, {"name": "readout_error", "value": 0.03489999999999993, "date": "2019-04-22T01:29:47+00:00", "unit": ""}], [ {"name": "T1", "value": 68.14048367144501, "date": "2019-04-22T01:30:15+00:00", "unit": "\u00b5s"}, {"name": "T2", "value": 56.95903203933663, "date": "2019-04-22T01:34:36+00:00", "unit": "\u00b5s"}, {"name": "frequency", "value": 4.896209948700639, "date": "2019-04-22T03:26:08+00:00", "unit": "GHz"}, {"name": "readout_error", "value": 0.19589999999999996, "date": "2019-04-22T01:29:47+00:00", "unit": ""}], ], "backend_name": "mock_4q", "backend_version": "1.0.0"} return BackendProperties.from_dict(properties) def test_truncate_ideal_sparse_circuit(self): """Test qubit truncation for large circuit with unused qubits.""" # Circuit that uses just 2-qubits circuit = QuantumCircuit(50, 2) circuit.x(10) circuit.x(20) circuit.measure(10, 0) circuit.measure(20, 1) qasm_sim = Aer.get_backend('qasm_simulator') backend_options = self.BACKEND_OPTS.copy() backend_options["truncate_verbose"] = True backend_options['optimize_ideal_threshold'] = 1 backend_options['optimize_noise_threshold'] = 1 result = execute(circuit, qasm_sim, shots=100, **backend_options).result() metadata = result.results[0].metadata self.assertTrue('truncate_qubits' in metadata, msg="truncate_qubits must work.") active_qubits = sorted(metadata['truncate_qubits'].get('active_qubits', [])) mapping = sorted(metadata['truncate_qubits'].get('mapping', [])) self.assertEqual(active_qubits, [10, 20]) self.assertIn(mapping, [[[10, 0], [20, 1]], [[10, 1], [20, 0]]]) def test_truncate_nonlocal_noise(self): """Test qubit truncation with non-local noise.""" # Circuit that uses just 2-qubits circuit = QuantumCircuit(10, 1) circuit.x(5) circuit.measure(5, 0) # Add non-local 2-qubit depolarizing error # that acts on qubits [4, 6] when X applied to qubit 5 noise_model = NoiseModel() error = depolarizing_error(0.1, 2) noise_model.add_nonlocal_quantum_error(error, ['x'], [5], [4, 6]) qasm_sim = Aer.get_backend('qasm_simulator') backend_options = self.BACKEND_OPTS.copy() backend_options["truncate_verbose"] = True backend_options['optimize_ideal_threshold'] = 1 backend_options['optimize_noise_threshold'] = 1 result = execute(circuit, qasm_sim, shots=100, noise_model=noise_model, **backend_options).result() metadata = result.results[0].metadata self.assertTrue('truncate_qubits' in metadata, msg="truncate_qubits must work.") active_qubits = sorted(metadata['truncate_qubits'].get('active_qubits', [])) mapping = metadata['truncate_qubits'].get('mapping', []) active_remapped = sorted([i[1] for i in mapping if i[0] in active_qubits]) self.assertEqual(active_qubits, [4, 5, 6]) self.assertEqual(active_remapped, [0, 1, 2]) def test_truncate(self): """Test truncation with noise model option""" circuit = self.create_circuit_for_truncate() qasm_sim = Aer.get_backend('qasm_simulator') backend_options = self.BACKEND_OPTS.copy() backend_options["truncate_verbose"] = True backend_options['optimize_ideal_threshold'] = 1 backend_options['optimize_noise_threshold'] = 1 result = execute(circuit, qasm_sim, noise_model=NoiseModel.from_backend(self.device_properties()), shots=100, coupling_map=[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 0]], # 10-qubit device **backend_options).result() self.assertTrue('truncate_qubits' in result.to_dict()['results'][0]['metadata'], msg="truncate_qubits must work.") def test_no_truncate(self): """Test truncation with noise model option""" circuit = self.create_circuit_for_truncate() qasm_sim = Aer.get_backend('qasm_simulator') backend_options = self.BACKEND_OPTS.copy() backend_options["truncate_verbose"] = True backend_options['optimize_ideal_threshold'] = 1 backend_options['optimize_noise_threshold'] = 1 result = execute(circuit, qasm_sim, noise_model=NoiseModel.from_backend(self.device_properties()), shots=100, coupling_map=[[1, 0], [1, 2], [1, 3], [2, 0], [2, 1], [2, 3], [3, 0], [3, 1], [3, 2]], # 4-qubit device **backend_options).result() self.assertFalse('truncate_qubits' in result.to_dict()['results'][0]['metadata'], msg="truncate_qubits must work.") def test_truncate_disable(self): """Test explicitly disabling truncation with noise model option""" circuit = self.create_circuit_for_truncate() qasm_sim = Aer.get_backend('qasm_simulator') backend_options = self.BACKEND_OPTS.copy() backend_options["truncate_verbose"] = True backend_options["truncate_enable"] = False backend_options['optimize_ideal_threshold'] = 1 backend_options['optimize_noise_threshold'] = 1 result = execute(circuit, qasm_sim, noise_model=NoiseModel.from_backend(self.device_properties()), shots=100, coupling_map=[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 0]], # 10-qubit device **backend_options).result() self.assertFalse('truncate_qubits' in result.to_dict()['results'][0]['metadata'], msg="truncate_qubits must not work.")
class QasmCliffordTestsMinimalBasis: """QasmSimulator Clifford gate tests in minimam U,CX basis.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test h-gate # --------------------------------------------------------------------- def test_h_gate_deterministic_minimal_basis_gates(self): """Test h-gate gate circuits compiling to u3,cx""" shots = 100 circuits = ref_1q_clifford.h_gate_circuits_deterministic( final_measure=True) targets = ref_1q_clifford.h_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) def test_h_gate_nondeterministic_minimal_basis_gates(self): """Test h-gate gate circuits compiling to u3,cx""" shots = 4000 circuits = ref_1q_clifford.h_gate_circuits_nondeterministic( final_measure=True) targets = ref_1q_clifford.h_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) # --------------------------------------------------------------------- # Test x-gate # --------------------------------------------------------------------- def test_x_gate_deterministic_minimal_basis_gates(self): """Test x-gate gate circuits compiling to u3,cx""" shots = 100 circuits = ref_1q_clifford.x_gate_circuits_deterministic( final_measure=True) targets = ref_1q_clifford.x_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx']) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) # --------------------------------------------------------------------- # Test z-gate # --------------------------------------------------------------------- def test_z_gate_deterministic_minimal_basis_gates(self): """Test z-gate gate circuits compiling to u3,cx""" shots = 100 circuits = ref_1q_clifford.z_gate_circuits_deterministic( final_measure=True) targets = ref_1q_clifford.z_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) # --------------------------------------------------------------------- # Test y-gate # --------------------------------------------------------------------- def test_y_gate_deterministic_minimal_basis_gates(self): """Test y-gate gate circuits compiling to u3,cx""" shots = 100 circuits = ref_1q_clifford.y_gate_circuits_deterministic( final_measure=True) targets = ref_1q_clifford.y_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) # --------------------------------------------------------------------- # Test s-gate # --------------------------------------------------------------------- def test_s_gate_deterministic_minimal_basis_gates(self): """Test s-gate gate circuits compiling to u3,cx""" shots = 100 circuits = ref_1q_clifford.s_gate_circuits_deterministic( final_measure=True) targets = ref_1q_clifford.s_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_s_gate_nondeterministic_minimal_basis_gates(self): """Test s-gate gate circuits compiling to u3,cx""" shots = 4000 circuits = ref_1q_clifford.s_gate_circuits_nondeterministic( final_measure=True) targets = ref_1q_clifford.s_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) # --------------------------------------------------------------------- # Test sdg-gate # --------------------------------------------------------------------- def test_sdg_gate_deterministic_minimal_basis_gates(self): """Test sdg-gate gate circuits compiling to u3,cx""" shots = 100 circuits = ref_1q_clifford.sdg_gate_circuits_deterministic( final_measure=True) targets = ref_1q_clifford.sdg_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_sdg_gate_nondeterministic_minimal_basis_gates(self): """Test sdg-gate gate circuits compiling to u3,cx""" shots = 4000 circuits = ref_1q_clifford.sdg_gate_circuits_nondeterministic( final_measure=True) targets = ref_1q_clifford.sdg_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) # --------------------------------------------------------------------- # Test cx-gate # --------------------------------------------------------------------- def test_cx_gate_deterministic_minimal_basis_gates(self): """Test cx-gate gate circuits compiling to u3,cx""" shots = 100 circuits = ref_2q_clifford.cx_gate_circuits_deterministic( final_measure=True) targets = ref_2q_clifford.cx_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_cx_gate_nondeterministic_minimal_basis_gates(self): """Test cx-gate gate circuits compiling to u3,cx""" shots = 4000 circuits = ref_2q_clifford.cx_gate_circuits_nondeterministic( final_measure=True) targets = ref_2q_clifford.cx_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) # --------------------------------------------------------------------- # Test cz-gate # --------------------------------------------------------------------- def test_cz_gate_deterministic_minimal_basis_gates(self): """Test cz-gate gate circuits compiling to u3,cx""" shots = 100 circuits = ref_2q_clifford.cz_gate_circuits_deterministic( final_measure=True) targets = ref_2q_clifford.cz_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_cz_gate_nondeterministic_minimal_basis_gates(self): """Test cz-gate gate circuits compiling to u3,cx""" shots = 4000 circuits = ref_2q_clifford.cz_gate_circuits_nondeterministic( final_measure=True) targets = ref_2q_clifford.cz_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) # --------------------------------------------------------------------- # Test swap-gate # --------------------------------------------------------------------- def test_swap_gate_deterministic_minimal_basis_gates(self): """Test swap-gate gate circuits compiling to u3,cx""" shots = 100 circuits = ref_2q_clifford.swap_gate_circuits_deterministic( final_measure=True) targets = ref_2q_clifford.swap_gate_counts_deterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0) def test_swap_gate_nondeterministic_minimal_basis_gates(self): """Test swap-gate gate circuits compiling to u3,cx""" shots = 4000 circuits = ref_2q_clifford.swap_gate_circuits_nondeterministic( final_measure=True) targets = ref_2q_clifford.swap_gate_counts_nondeterministic(shots) job = execute(circuits, self.SIMULATOR, shots=shots, basis_gates=['u3', 'cx'], **self.BACKEND_OPTS) result = job.result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
class QasmFusionTests: """QasmSimulator fusion tests.""" SIMULATOR = QasmSimulator() def create_statevector_circuit(self): """ Creates a simple circuit for running in the statevector """ qr = QuantumRegister(5) cr = ClassicalRegister(5) circuit = QuantumCircuit(qr, cr) circuit.u3(0.1, 0.1, 0.1, qr[0]) circuit.barrier(qr) circuit.x(qr[0]) circuit.barrier(qr) circuit.x(qr[1]) circuit.barrier(qr) circuit.x(qr[0]) circuit.barrier(qr) circuit.u3(0.1, 0.1, 0.1, qr[0]) circuit.barrier(qr) circuit.measure(qr, cr) return circuit def noise_model_depol(self): """ Creates a new noise model for testing purposes """ readout_error = [0.01, 0.1] params = {'u3': (1, 0.001), 'cx': (2, 0.02)} noise = NoiseModel() readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise.add_all_qubit_readout_error(ReadoutError(readout)) for gate, (num_qubits, gate_error) in params.items(): noise.add_all_qubit_quantum_error( depolarizing_error(gate_error, num_qubits), gate) return noise def noise_model_kraus(self): """ Creates a new noise model for testing purposes """ readout_error = [0.01, 0.1] params = {'u3': (1, 0.001), 'cx': (2, 0.02)} noise = NoiseModel() readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise.add_all_qubit_readout_error(ReadoutError(readout)) for gate, (num_qubits, gate_error) in params.items(): noise.add_all_qubit_quantum_error( amplitude_damping_error(gate_error, num_qubits), gate) return noise def fusion_options(self, enabled=None, threshold=None, verbose=None): """Return default backend_options dict.""" backend_options = self.BACKEND_OPTS.copy() if enabled is not None: backend_options['fusion_enable'] = enabled if verbose is not None: backend_options['fusion_verbose'] = verbose if threshold is not None: backend_options['fusion_threshold'] = threshold return backend_options def fusion_metadata(self, result): """Return fusion metadata dict""" metadata = result.results[0].metadata return metadata.get('fusion', {}) def test_fusion_theshold(self): """Test fusion threhsold""" shots = 100 threshold = 6 backend_options = self.fusion_options(enabled=True, threshold=threshold) with self.subTest(msg='below fusion threshold'): circuit = transpile(QFT(threshold - 1), self.SIMULATOR, basis_gates=['u1', 'u2', 'u3', 'cx', 'cz'], optimization_level=0) circuit.measure_all() qobj = assemble(circuit, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, **backend_options).result() self.assertSuccess(result) #meta = self.fusion_metadata(result) #self.assertTrue(meta.get('enabled')) #self.assertFalse(meta.get('applied')) with self.subTest(msg='at fusion threshold'): circuit = transpile(QFT(threshold), self.SIMULATOR, basis_gates=['u1', 'u2', 'u3', 'cx', 'cz'], optimization_level=0) circuit.measure_all() qobj = assemble(circuit, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, **backend_options).result() self.assertSuccess(result) #meta = self.fusion_metadata(result) #self.assertTrue(meta.get('enabled')) #self.assertFalse(meta.get('applied')) with self.subTest(msg='above fusion threshold'): circuit = transpile(QFT(threshold + 1), self.SIMULATOR, basis_gates=['u1', 'u2', 'u3', 'cx', 'cz'], optimization_level=0) circuit.measure_all() qobj = assemble(circuit, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, **backend_options).result() self.assertSuccess(result) #meta = self.fusion_metadata(result) #self.assertTrue(meta.get('enabled')) #self.assertTrue(meta.get('applied')) def test_fusion_verbose(self): """Test Fusion with verbose option""" circuit = self.create_statevector_circuit() shots = 100 qobj = assemble(transpile([circuit], self.SIMULATOR, optimization_level=0), shots=shots) with self.subTest(msg='verbose enabled'): backend_options = self.fusion_options(enabled=True, verbose=True, threshold=1) result = self.SIMULATOR.run(qobj, **backend_options).result() # Assert fusion applied succesfully self.assertSuccess(result) #meta = self.fusion_metadata(result) #self.assertTrue(meta.get('applied', False)) # Assert verbose meta data in output #self.assertIn('input_ops', meta) #self.assertIn('output_ops', meta) with self.subTest(msg='verbose disabled'): backend_options = self.fusion_options(enabled=True, verbose=False, threshold=1) result = self.SIMULATOR.run(qobj, **backend_options).result() # Assert fusion applied succesfully self.assertSuccess(result) #meta = self.fusion_metadata(result) #self.assertTrue(meta.get('applied', False)) # Assert verbose meta data not in output #self.assertNotIn('input_ops', meta) #self.assertNotIn('output_ops', meta) with self.subTest(msg='verbose default'): backend_options = self.fusion_options(enabled=True, threshold=1) result = self.SIMULATOR.run(qobj, **backend_options).result() # Assert fusion applied succesfully self.assertSuccess(result) #meta = self.fusion_metadata(result) #self.assertTrue(meta.get('applied', False)) # Assert verbose meta data not in output #self.assertNotIn('input_ops', meta) #self.assertNotIn('output_ops', meta) #def test_kraus_noise_fusion(self): # """Test Fusion with kraus noise model option""" # shots = 100 # circuit = self.create_statevector_circuit() # noise_model = self.noise_model_kraus() # circuit = transpile([circuit], # backend=self.SIMULATOR, # basis_gates=noise_model.basis_gates, # optimization_level=0) # qobj = assemble([circuit], # self.SIMULATOR, # shots=shots, # seed_simulator=1) # backend_options = self.fusion_options(enabled=True, threshold=1) # result = self.SIMULATOR.run(qobj, # noise_model=noise_model, # **backend_options).result() # method = result.results[0].metadata.get('method') # #meta = self.fusion_metadata(result) # if method in ['density_matrix', 'density_matrix_thrust', 'density_matrix_gpu']: # target_method = 'superop' # else: # target_method = 'kraus' # self.assertSuccess(result) # #self.assertTrue(meta.get('applied', False), # # msg='fusion should have been applied.') # #self.assertEqual(meta.get('method', None), target_method) #def test_non_kraus_noise_fusion(self): # """Test Fusion with non-kraus noise model option""" # shots = 100 # circuit = self.create_statevector_circuit() # noise_model = self.noise_model_depol() # circuit = transpile([circuit], # backend=self.SIMULATOR, # basis_gates=noise_model.basis_gates, # optimization_level=0) # qobj = assemble([circuit], # self.SIMULATOR, # shots=shots, # seed_simulator=1) # backend_options = self.fusion_options(enabled=True, threshold=1) # result = self.SIMULATOR.run(qobj, # noise_model=noise_model, # **backend_options).result() # #meta = self.fusion_metadata(result) # method = result.results[0].metadata.get('method') # if method in ['density_matrix', 'density_matrix_thrust', 'density_matrix_gpu']: # target_method = 'superop' # else: # target_method = 'unitary' # self.assertSuccess(result) # #self.assertTrue(meta.get('applied', False), # # msg='fusion should have been applied.') # #self.assertEqual(meta.get('method', None), target_method) def test_control_fusion(self): """Test Fusion enable/disable option""" shots = 100 circuit = transpile(self.create_statevector_circuit(), backend=self.SIMULATOR, optimization_level=0) qobj = assemble([circuit], self.SIMULATOR, shots=shots, seed_simulator=1) with self.subTest(msg='fusion enabled'): backend_options = self.fusion_options(enabled=True, threshold=1) result = self.SIMULATOR.run(copy.deepcopy(qobj), **backend_options).result() #meta = self.fusion_metadata(result) self.assertSuccess(result) #self.assertTrue(meta.get('enabled')) #self.assertTrue(meta.get('applied', False)) with self.subTest(msg='fusion disabled'): backend_options = backend_options = self.fusion_options( enabled=False, threshold=1) result = self.SIMULATOR.run(copy.deepcopy(qobj), **backend_options).result() #meta = self.fusion_metadata(result) self.assertSuccess(result) #self.assertFalse(meta.get('enabled')) with self.subTest(msg='fusion default'): backend_options = self.fusion_options() result = self.SIMULATOR.run(copy.deepcopy(qobj), **backend_options).result() #meta = self.fusion_metadata(result) self.assertSuccess(result) #self.assertTrue(meta.get('enabled')) #self.assertFalse(meta.get('applied', False), msg=meta) def test_fusion_operations(self): """Test Fusion enable/disable option""" shots = 100 num_qubits = 8 qr = QuantumRegister(num_qubits) cr = ClassicalRegister(num_qubits) circuit = QuantumCircuit(qr, cr) circuit.h(qr) circuit.barrier(qr) circuit.u3(0.1, 0.1, 0.1, qr[0]) circuit.barrier(qr) circuit.u3(0.1, 0.1, 0.1, qr[1]) circuit.barrier(qr) circuit.cx(qr[1], qr[0]) circuit.barrier(qr) circuit.u3(0.1, 0.1, 0.1, qr[0]) circuit.barrier(qr) circuit.u3(0.1, 0.1, 0.1, qr[1]) circuit.barrier(qr) circuit.u3(0.1, 0.1, 0.1, qr[3]) circuit.barrier(qr) circuit.x(qr[0]) circuit.barrier(qr) circuit.x(qr[1]) circuit.barrier(qr) circuit.x(qr[0]) circuit.barrier(qr) circuit.x(qr[1]) circuit.barrier(qr) circuit.cx(qr[2], qr[3]) circuit.barrier(qr) circuit.u3(0.1, 0.1, 0.1, qr[3]) circuit.barrier(qr) circuit.u3(0.1, 0.1, 0.1, qr[3]) circuit.barrier(qr) circuit.x(qr[0]) circuit.barrier(qr) circuit.x(qr[1]) circuit.barrier(qr) circuit.x(qr[0]) circuit.barrier(qr) circuit.x(qr[1]) circuit.barrier(qr) circuit.cx(qr[2], qr[3]) circuit.barrier(qr) circuit.u3(0.1, 0.1, 0.1, qr[3]) circuit.barrier(qr) circuit.u3(0.1, 0.1, 0.1, qr[3]) circuit.barrier(qr) circuit.measure(qr, cr) circuit = transpile(circuit, backend=self.SIMULATOR, optimization_level=0) qobj = assemble([circuit], self.SIMULATOR, shots=shots, seed_simulator=1) backend_options = self.fusion_options(enabled=False, threshold=1) result_disabled = self.SIMULATOR.run(qobj, **backend_options).result() #meta_disabled = self.fusion_metadata(result_disabled) backend_options = self.fusion_options(enabled=True, threshold=1) result_enabled = self.SIMULATOR.run(qobj, **backend_options).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('enabled')) #self.assertTrue(meta_enabled.get('enabled')) #self.assertTrue(meta_enabled.get('applied')) #self.assertDictAlmostEqual(result_enabled.get_counts(circuit), # result_disabled.get_counts(circuit), # delta=0.0, # msg="fusion for qft 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_fusion_qft(self): """Test Fusion with qft""" shots = 100 num_qubits = 8 circuit = transpile(QFT(num_qubits), backend=self.SIMULATOR, basis_gates=['u1', 'u2', 'u3', 'cx', 'cz'], optimization_level=0) circuit.measure_all() qobj = assemble([circuit], self.SIMULATOR, shots=shots, seed_simulator=1) backend_options = self.fusion_options(enabled=False, threshold=1) result_disabled = self.SIMULATOR.run(qobj, **backend_options).result() #meta_disabled = self.fusion_metadata(result_disabled) backend_options = self.fusion_options(enabled=True, threshold=1) result_enabled = self.SIMULATOR.run(qobj, **backend_options).result()
class QasmMultiQubitMeasureTests: """QasmSimulator measure tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test multi-qubit measure qobj instruction # --------------------------------------------------------------------- def test_measure_deterministic_multi_qubit_with_sampling(self): """Test QasmSimulator multi-qubit measure with deterministic counts with sampling""" shots = 100 circuits = ref_measure.multiqubit_measure_circuits_deterministic( allow_sampling=True) target_counts = ref_measure.multiqubit_measure_counts_deterministic( shots) target_memory = ref_measure.multiqubit_measure_memory_deterministic( shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots, memory=True) result = self.SIMULATOR.run(qobj, **self.BACKEND_OPTS).result() self.assertSuccess(result) self.compare_counts(result, circuits, target_counts, delta=0) self.compare_memory(result, circuits, target_memory) #self.compare_result_metadata(result, circuits, "measure_sampling", True) def test_measure_deterministic_multi_qubit_without_sampling(self): """Test QasmSimulator multi-qubit measure with deterministic counts without sampling""" shots = 100 circuits = ref_measure.multiqubit_measure_circuits_deterministic( allow_sampling=False) target_counts = ref_measure.multiqubit_measure_counts_deterministic( shots) target_memory = ref_measure.multiqubit_measure_memory_deterministic( shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots, memory=True) result = self.SIMULATOR.run(qobj, **self.BACKEND_OPTS).result() self.compare_counts(result, circuits, target_counts, delta=0) self.compare_memory(result, circuits, target_memory) #self.compare_result_metadata(result, circuits, "measure_sampling", False) def test_measure_nondeterministic_multi_qubit_with_sampling(self): """Test QasmSimulator measure with non-deterministic counts""" shots = 4000 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, **self.BACKEND_OPTS).result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) #self.compare_result_metadata(result, circuits, "measure_sampling", True) def test_measure_nondeterministic_multi_qubit_without_sampling(self): """Test QasmSimulator measure with non-deterministic counts""" shots = 4000 circuits = ref_measure.multiqubit_measure_circuits_nondeterministic( allow_sampling=False) targets = ref_measure.multiqubit_measure_counts_nondeterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, **self.BACKEND_OPTS).result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots)
class QasmDelayMeasureTests: """QasmSimulator delay measure sampling optimization tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} def delay_measure_circuit(self): """Test circuit that allows measure delay optimization""" qr = QuantumRegister(2, 'qr') cr = ClassicalRegister(2, 'cr') circuit = QuantumCircuit(qr, cr) circuit.x(0) circuit.measure(0, 0) circuit.barrier([0, 1]) circuit.x(1) circuit.measure(0, 1) return circuit def test_delay_measure_enable(self): """Test measure sampling works with delay measure optimization""" # Circuit that allows delay measure circuit = self.delay_measure_circuit() shots = 100 qobj = assemble([circuit], self.SIMULATOR, shots=shots, seed_simulator=1) # Delay measure default backend_options = self.BACKEND_OPTS.copy() backend_options['optimize_ideal_threshold'] = 0 result = self.SIMULATOR.run( qobj, **backend_options).result() self.assertSuccess(result) #metadata = result.results[0].metadata #self.assertTrue(metadata.get('measure_sampling')) # Delay measure enabled backend_options = self.BACKEND_OPTS.copy() backend_options['delay_measure_enable'] = True backend_options['optimize_ideal_threshold'] = 0 result = self.SIMULATOR.run( qobj, **backend_options).result() self.assertSuccess(result) #metadata = result.results[0].metadata #self.assertTrue(metadata.get('measure_sampling')) # Delay measure disabled backend_options = self.BACKEND_OPTS.copy() backend_options['delay_measure_enable'] = False backend_options['optimize_ideal_threshold'] = 0 result = self.SIMULATOR.run( qobj, **backend_options).result() self.assertSuccess(result) #metadata = result.results[0].metadata #self.assertFalse(metadata.get('measure_sampling')) def test_delay_measure_verbose(self): """Test delay measure with verbose option""" circuit = self.delay_measure_circuit() shots = 100 qobj = assemble([circuit], self.SIMULATOR, shots=shots, seed_simulator=1) # Delay measure verbose enabled backend_options = self.BACKEND_OPTS.copy() backend_options['delay_measure_enable'] = True backend_options['delay_measure_verbose'] = True backend_options['optimize_ideal_threshold'] = 0 result = self.SIMULATOR.run( qobj, **backend_options).result() self.assertSuccess(result) #metadata = result.results[0].metadata #self.assertIn('delay_measure_verbose', metadata) # Delay measure verbose disabled backend_options = self.BACKEND_OPTS.copy() backend_options['delay_measure_enable'] = True backend_options['delay_measure_verbose'] = False backend_options['optimize_ideal_threshold'] = 0 result = self.SIMULATOR.run( qobj, **backend_options).result() self.assertSuccess(result) #metadata = result.results[0].metadata #self.assertNotIn('delay_measure_verbose', metadata) # Delay measure verbose default backend_options = self.BACKEND_OPTS.copy() backend_options['delay_measure_enable'] = True backend_options['optimize_ideal_threshold'] = 1 backend_options['optimize_noise_threshold'] = 1 result = self.SIMULATOR.run( qobj, **backend_options).result() self.assertSuccess(result)
class QasmMethodTests: """QasmSimulator method option tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} # --------------------------------------------------------------------- # Test Clifford circuits with clifford and non-clifford noise # --------------------------------------------------------------------- def test_backend_method_clifford_circuits(self): """Test statevector method is used for Clifford circuit""" # Test circuits shots = 100 circuits = ref_2q_clifford.cz_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, **self.BACKEND_OPTS).result() success = getattr(result, 'success', False) self.assertTrue(success) # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method != 'automatic': self.compare_result_metadata(result, circuits, 'method', method) else: self.compare_result_metadata(result, circuits, 'method', 'stabilizer') def test_backend_method_clifford_circuits_and_reset_noise(self): """Test statevector method is used for Clifford circuit""" # Test noise model noise_circs = [[{ "name": "reset", "qubits": [0] }], [{ "name": "id", "qubits": [0] }]] noise_probs = [0.5, 0.5] error = QuantumError(zip(noise_circs, noise_probs)) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error( error, ['id', 'x', 'y', 'z', 'h', 's', 'sdg']) # Test circuits shots = 100 circuits = ref_2q_clifford.cz_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, noise_model=noise_model, **self.BACKEND_OPTS).result() success = getattr(result, 'success', False) self.assertTrue(success) # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method != 'automatic': self.compare_result_metadata(result, circuits, 'method', method) else: self.compare_result_metadata(result, circuits, 'method', 'stabilizer') def test_backend_method_clifford_circuits_and_pauli_noise(self): """Test statevector method is used for Clifford circuit""" # Noise Model error = pauli_error([['XX', 0.5], ['II', 0.5]], standard_gates=True) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, ['cz', 'cx']) # Test circuits shots = 100 circuits = ref_2q_clifford.cz_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, **self.BACKEND_OPTS).result() success = getattr(result, 'success', False) self.assertTrue(success) # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method != 'automatic': self.compare_result_metadata(result, circuits, 'method', method) else: self.compare_result_metadata(result, circuits, 'method', 'stabilizer') def test_backend_method_clifford_circuits_and_unitary_noise(self): """Test statevector method is used for Clifford circuit""" # Noise Model error = pauli_error([['XX', 0.5], ['II', 0.5]], standard_gates=False) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, ['cz', 'cx']) # Test circuits shots = 100 circuits = ref_2q_clifford.cz_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, noise_model=noise_model, **self.BACKEND_OPTS).result() success = getattr(result, 'success', False) # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method == 'stabilizer': self.assertFalse(success) else: self.assertTrue(success) if method == 'automatic': target_method = 'density_matrix' else: target_method = method self.compare_result_metadata(result, circuits, 'method', target_method) def test_backend_method_clifford_circuits_and_kraus_noise(self): """Test statevector method is used for Clifford circuit""" # Noise Model error = amplitude_damping_error(0.5) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error( error, ['id', 'x', 'y', 'z', 'h', 's', 'sdg']) # Test circuits shots = 100 circuits = ref_2q_clifford.cz_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, noise_model=noise_model, **self.BACKEND_OPTS).result() success = getattr(result, 'success', False) # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method == 'stabilizer': self.assertFalse(success) else: self.assertTrue(success) if method == 'automatic': target_method = 'density_matrix' else: target_method = method self.compare_result_metadata(result, circuits, 'method', target_method) # --------------------------------------------------------------------- # Test non-Clifford circuits with clifford and non-clifford noise # --------------------------------------------------------------------- def test_backend_method_nonclifford_circuits(self): """Test statevector method is used for Clifford circuit""" # Test circuits shots = 100 circuits = ref_non_clifford.ccx_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, **self.BACKEND_OPTS).result() success = getattr(result, 'success', False) # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method == 'stabilizer': self.assertFalse(success) else: self.assertTrue(success) if method == 'automatic': target_method = 'statevector' else: target_method = method self.compare_result_metadata(result, circuits, 'method', target_method) def test_backend_method_nonclifford_circuit_and_reset_noise(self): """Test statevector method is used for Clifford circuit""" # Test noise model noise_circs = [[{ "name": "reset", "qubits": [0] }], [{ "name": "id", "qubits": [0] }]] noise_probs = [0.5, 0.5] error = QuantumError(zip(noise_circs, noise_probs)) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error( error, ['id', 'x', 'y', 'z', 'h', 's', 'sdg']) # Test circuits shots = 100 circuits = ref_non_clifford.ccx_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, noise_model=noise_model, **self.BACKEND_OPTS).result() success = getattr(result, 'success', False) # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method == 'stabilizer': self.assertFalse(success) else: self.assertTrue self.assertTrue(success) if method == 'automatic': target_method = 'density_matrix' else: target_method = method self.compare_result_metadata(result, circuits, 'method', target_method) def test_backend_method_nonclifford_circuit_and_pauli_noise(self): """Test statevector method is used for Clifford circuit""" # Noise Model error = pauli_error([['XX', 0.5], ['II', 0.5]], standard_gates=True) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, ['cz', 'cx']) # Test circuits shots = 100 circuits = ref_non_clifford.ccx_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, noise_model=noise_model, **self.BACKEND_OPTS).result() success = getattr(result, 'success', False) # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method == 'stabilizer': self.assertFalse(success) else: self.assertTrue(success) if method == 'automatic': target_method = 'density_matrix' else: target_method = method self.compare_result_metadata(result, circuits, 'method', target_method) def test_backend_method_nonclifford_circuit_and_unitary_noise(self): """Test statevector method is used for Clifford circuit""" # Noise Model error = pauli_error([['XX', 0.5], ['II', 0.5]], standard_gates=False) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, ['cz', 'cx']) # Test circuits shots = 100 circuits = ref_non_clifford.ccx_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, noise_model=noise_model, **self.BACKEND_OPTS).result() success = getattr(result, 'success', False) # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method == 'stabilizer': self.assertFalse(success) else: self.assertTrue(success) if method == 'automatic': target_method = 'density_matrix' else: target_method = method self.compare_result_metadata(result, circuits, 'method', target_method) def test_backend_method_nonclifford_circuit_and_kraus_noise(self): """Test statevector method is used for Clifford circuit""" # Noise Model error = amplitude_damping_error(0.5) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error( error, ['id', 'x', 'y', 'z', 'h', 's', 'sdg']) # Test circuits shots = 100 circuits = ref_non_clifford.ccx_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, noise_model=noise_model, **self.BACKEND_OPTS).result() success = getattr(result, 'success', False) # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method == 'stabilizer': self.assertFalse(success) else: self.assertTrue(success) if method == 'automatic': target_method = 'density_matrix' else: target_method = method self.compare_result_metadata(result, circuits, 'method', target_method)
class QasmMeasureTests: """QasmSimulator 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) target_counts = ref_measure.measure_counts_deterministic(shots) target_memory = ref_measure.measure_memory_deterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots, memory=True) result = self.SIMULATOR.run(qobj, **self.BACKEND_OPTS).result() self.assertSuccess(result) self.compare_counts(result, circuits, target_counts, delta=0) self.compare_memory(result, circuits, target_memory) #self.compare_result_metadata(result, circuits, "measure_sampling", True) def test_measure_deterministic_without_sampling(self): """Test QasmSimulator measure with deterministic counts without sampling""" shots = 100 circuits = ref_measure.measure_circuits_deterministic( allow_sampling=False) target_counts = ref_measure.measure_counts_deterministic(shots) target_memory = ref_measure.measure_memory_deterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots, memory=True) result = self.SIMULATOR.run(qobj, **self.BACKEND_OPTS).result() self.assertSuccess(result) self.compare_counts(result, circuits, target_counts, delta=0) self.compare_memory(result, circuits, target_memory) #self.compare_result_metadata(result, circuits, "measure_sampling", False) def test_measure_nondeterministic_with_sampling(self): """Test QasmSimulator measure with non-deterministic counts with sampling""" shots = 4000 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, **self.BACKEND_OPTS).result() self.assertSuccess(result) 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) def test_measure_nondeterministic_without_sampling(self): """Test QasmSimulator measure with non-deterministic counts without sampling""" shots = 4000 circuits = ref_measure.measure_circuits_nondeterministic( allow_sampling=False) targets = ref_measure.measure_counts_nondeterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, **self.BACKEND_OPTS).result() self.assertSuccess(result) self.compare_counts(result, circuits, targets, delta=0.05 * shots) #self.compare_result_metadata(result, circuits, "measure_sampling", False) def test_measure_sampling_with_readouterror(self): """Test QasmSimulator measure with deterministic counts with sampling and readout-error""" readout_error = [0.01, 0.1] noise_model = NoiseModel() readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise_model.add_all_qubit_readout_error(ReadoutError(readout)) shots = 1000 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, noise_model=noise_model, **self.BACKEND_OPTS).result() self.assertSuccess(result) #self.compare_result_metadata(result, circuits, "measure_sampling", True) def test_measure_sampling_with_quantum_noise(self): """Test QasmSimulator measure with deterministic counts with sampling and readout-error""" readout_error = [0.01, 0.1] noise_model = NoiseModel() depolarizing = {'u3': (1, 0.001), 'cx': (2, 0.02)} readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise_model.add_all_qubit_readout_error(ReadoutError(readout)) for gate, (num_qubits, gate_error) in depolarizing.items(): noise_model.add_all_qubit_quantum_error( depolarizing_error(gate_error, num_qubits), gate) shots = 1000 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, noise_model=noise_model, **self.BACKEND_OPTS).result() self.assertSuccess(result)
class QasmThreadManagementTests: """QasmSimulator thread tests.""" SIMULATOR = QasmSimulator() BACKEND_OPTS = {} def backend_options_parallel(self, total_threads=None, state_threads=None, shot_threads=None, exp_threads=None): """Backend options with thread manangement.""" opts = self.BACKEND_OPTS.copy() if total_threads: opts['max_parallel_threads'] = total_threads else: opts['max_parallel_threads'] = 0 if shot_threads: opts['max_parallel_shots'] = shot_threads if state_threads: opts['max_parallel_state_update'] = state_threads if exp_threads: opts['max_parallel_experiments'] = exp_threads return opts def dummy_noise_model(self): """Return dummy noise model for dummy circuit""" noise_model = NoiseModel() error = pauli_error([('X', 0.25), ('I', 0.75)]) noise_model.add_all_qubit_quantum_error(error, 'x') return noise_model def dummy_circuit(self, num_qubits): """Dummy circuit for testing thread settings""" circ = QuantumCircuit(num_qubits, num_qubits) circ.x(range(num_qubits)) circ.measure(range(num_qubits), range(num_qubits)) return circ def measure_in_middle_circuit(self, num_qubits): """Dummy circuit for testing thread settings""" circ = QuantumCircuit(num_qubits, num_qubits) circ.measure(range(num_qubits), range(num_qubits)) circ.x(range(num_qubits)) circ.measure(range(num_qubits), range(num_qubits)) return circ def threads_used(self, result): """Return a list of threads used for each execution""" exp_threads = getattr(result, 'metadata', {}).get('parallel_experiments', 1) threads = [] for exp_result in getattr(result, 'results', []): exp_meta = getattr(exp_result, 'metadata', {}) shot_threads = exp_meta.get('parallel_shots', 1) state_threads = exp_meta.get('parallel_state_update', 1) threads.append({ 'experiments': exp_threads, 'shots': shot_threads, 'state_update': state_threads, 'total': exp_threads * shot_threads * state_threads }) return threads 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 available_threads(self): """"Return the threads reported by the simulator""" opts = self.backend_options_parallel() result = execute(self.dummy_circuit(1), self.SIMULATOR, shots=1, **opts).result() return self.threads_used(result)[0]['total'] @requires_omp @requires_multiprocessing def test_parallel_thread_defaults(self): """Test parallel thread assignment defaults""" opts = self.backend_options_parallel() max_threads = self.available_threads() # Test single circuit, no noise # Parallel experiments and shots should always be 1 result = execute(self.dummy_circuit(1), self.SIMULATOR, shots=10 * max_threads, **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': 1, 'state_update': max_threads, 'total': max_threads } self.assertEqual(threads, target) # Test single circuit, with noise # Parallel experiments should always be 1 # parallel shots should be greater than 1 result = execute(self.dummy_circuit(1), self.SIMULATOR, shots=10 * max_threads, noise_model=self.dummy_noise_model(), **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': max_threads, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) # Test single circuit, with measure in middle, no noise # Parallel experiments should always be 1 # parallel shots should be greater than 1 result = execute(self.measure_in_middle_circuit(1), self.SIMULATOR, shots=10 * max_threads, **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': max_threads, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuit, no noise # Parallel experiments always be 1 # parallel shots should always be 1 result = execute(max_threads * [self.dummy_circuit(1)], self.SIMULATOR, shots=10 * max_threads, **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': 1, 'state_update': max_threads, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuits, with noise # Parallel experiments should always be 1 # parallel shots should be greater than 1 result = execute(max_threads * [self.dummy_circuit(1)], self.SIMULATOR, shots=10 * max_threads, noise_model=self.dummy_noise_model(), **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': max_threads, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuit, with measure in middle, no noise # Parallel experiments should always be 1 # parallel shots should be greater than 1 result = execute(max_threads * [self.measure_in_middle_circuit(1)], self.SIMULATOR, shots=10 * max_threads, noise_model=self.dummy_noise_model(), **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': max_threads, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) @requires_omp @requires_multiprocessing def test_parallel_thread_assignment_priority(self): """Test parallel thread assignment priority""" # If we set all values to max test output # We intentionally set the max shot and experiment threads to # twice the max threads to check they are limited correctly for custom_max_threads in [0, 1, 2, 4]: opts = self.backend_options_parallel() opts['max_parallel_threads'] = custom_max_threads opts['max_parallel_experiments'] = 2 * custom_max_threads opts['max_parallel_shots'] = 2 * custom_max_threads # Calculate actual max threads from custom max and CPU number max_threads = self.available_threads() if custom_max_threads > 0: max_threads = min(max_threads, custom_max_threads) # Test single circuit, no noise # Parallel experiments and shots should always be 1 result = execute(self.dummy_circuit(1), self.SIMULATOR, shots=10 * max_threads, **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': 1, 'state_update': max_threads, 'total': max_threads } self.assertEqual(threads, target) # Test single circuit, with noise # Parallel experiments should always be 1 # parallel shots should be greater than 1 result = execute(self.dummy_circuit(1), self.SIMULATOR, shots=10 * max_threads, noise_model=self.dummy_noise_model(), **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': max_threads, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) # Test single circuit, with measure in middle, no noise # Parallel experiments should always be 1 # parallel shots should be greater than 1 result = execute(self.measure_in_middle_circuit(1), self.SIMULATOR, shots=10 * max_threads, **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': max_threads, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuit, no noise # Parallel experiments always be greater than 1 # parallel shots should always be 1 result = execute(max_threads * [self.dummy_circuit(1)], self.SIMULATOR, shots=10 * max_threads, **opts).result() for threads in self.threads_used(result): target = { 'experiments': max_threads, 'shots': 1, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuits, with noise # Parallel experiments always be greater than 1 # parallel shots should always be 1 result = execute(max_threads * [self.dummy_circuit(1)], self.SIMULATOR, shots=10 * max_threads, noise_model=self.dummy_noise_model(), **opts).result() for threads in self.threads_used(result): target = { 'experiments': max_threads, 'shots': 1, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuit, with measure in middle, no noise # Parallel experiments always be greater than 1 # parallel shots should always be 1 result = execute(max_threads * [self.measure_in_middle_circuit(1)], self.SIMULATOR, shots=10 * max_threads, noise_model=self.dummy_noise_model(), **opts).result() for threads in self.threads_used(result): target = { 'experiments': max_threads, 'shots': 1, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) @requires_omp @requires_multiprocessing def test_parallel_experiment_thread_assignment(self): """Test parallel experiment thread assignment""" max_threads = self.available_threads() opts = self.backend_options_parallel(exp_threads=max_threads) # Test single circuit # Parallel experiments and shots should always be 1 result = execute(self.dummy_circuit(1), self.SIMULATOR, shots=10 * max_threads, **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': 1, 'state_update': max_threads, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuit, no noise # Parallel experiments should take priority result = execute(max_threads * [self.dummy_circuit(1)], self.SIMULATOR, shots=10 * max_threads, **opts).result() for threads in self.threads_used(result): target = { 'experiments': max_threads, 'shots': 1, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuits, with noise # Parallel experiments should take priority result = execute(max_threads * [self.dummy_circuit(1)], self.SIMULATOR, shots=10 * max_threads, noise_model=self.dummy_noise_model(), **opts).result() for threads in self.threads_used(result): target = { 'experiments': max_threads, 'shots': 1, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuit, with measure in middle, no noise # Parallel experiments should take priority result = execute(max_threads * [self.measure_in_middle_circuit(1)], self.SIMULATOR, shots=10 * max_threads, noise_model=self.dummy_noise_model(), **opts).result() for threads in self.threads_used(result): target = { 'experiments': max_threads, 'shots': 1, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuits, with memory limitation # NOTE: this assumes execution on statevector simulator # which required approx 2 MB for 16 qubit circuit. opts['max_memory_mb'] = 1 circuit = QuantumVolume(16, 1, seed=0) circuit.measure_all() result = execute(2 * [circuit], self.SIMULATOR, shots=10 * max_threads, **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': 1, 'state_update': max_threads, 'total': max_threads } self.assertEqual(threads, target) @requires_omp @requires_multiprocessing def test_parallel_shot_thread_assignment(self): """Test parallel shot thread assignment""" max_threads = self.available_threads() opts = self.backend_options_parallel(shot_threads=max_threads) # Test single circuit # Parallel experiments and shots should always be 1 result = execute(self.dummy_circuit(1), self.SIMULATOR, shots=10 * max_threads, **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': 1, 'state_update': max_threads, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuit, no noise # Parallel experiments and shots should always be 1 result = execute(max_threads * [self.dummy_circuit(1)], self.SIMULATOR, shots=10 * max_threads, **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': 1, 'state_update': max_threads, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuits, with noise # Parallel shots should take priority result = execute(max_threads * [self.dummy_circuit(1)], self.SIMULATOR, shots=10 * max_threads, noise_model=self.dummy_noise_model(), **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': max_threads, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuit, with measure in middle, no noise # Parallel shots should take priority result = execute(max_threads * [self.measure_in_middle_circuit(1)], self.SIMULATOR, shots=10 * max_threads, noise_model=self.dummy_noise_model(), **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': max_threads, 'state_update': 1, 'total': max_threads } self.assertEqual(threads, target) # Test multiple circuits, with memory limitation # NOTE: this assumes execution on statevector simulator # which required approx 2 MB for 16 qubit circuit. opts['max_memory_mb'] = 1 circuit = QuantumVolume(16, 1, seed=0) circuit.measure_all() result = execute(2 * [circuit], self.SIMULATOR, shots=10 * max_threads, **opts).result() for threads in self.threads_used(result): target = { 'experiments': 1, 'shots': 1, 'state_update': max_threads, 'total': max_threads } self.assertEqual(threads, target) @requires_omp @requires_multiprocessing 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_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")