From 807ac6f81b8a05e543822c7df343bdc3f99dab1a Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 25 Apr 2024 16:00:41 +0900 Subject: [PATCH] Revert "Replace example in README to using primitives" This reverts commit b536563851cdd51ea1db27176004b3c151ae4cd7. --- README.md | 104 +++++++++++++++++------------------------------------- 1 file changed, 32 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index 001aa87fdd..591a56f17b 100755 --- a/README.md +++ b/README.md @@ -46,85 +46,45 @@ the [contributing guide](CONTRIBUTING.md#building-with-gpu-support) for instructions on doing this. ## Simulating your first Qiskit circuit with Aer -Now that you have Aer installed, you can start simulating quantum circuits using primitives. Here is a basic example: +Now that you have Aer installed, you can start simulating quantum circuits with noise. Here is a basic example: ``` $ python ``` ```python -from qiskit import transpile -from qiskit.circuit.library import RealAmplitudes -from qiskit.quantum_info import SparsePauliOp +import qiskit from qiskit_aer import AerSimulator - -sim = AerSimulator() -# -------------------------- -# Simulating using estimator -#--------------------------- -from qiskit_aer.primitives import EstimatorV2 - -psi1 = transpile(RealAmplitudes(num_qubits=2, reps=2), sim, optimization_level=0) -psi2 = transpile(RealAmplitudes(num_qubits=2, reps=3), sim, optimization_level=0) - -H1 = SparsePauliOp.from_list([("II", 1), ("IZ", 2), ("XI", 3)]) -H2 = SparsePauliOp.from_list([("IZ", 1)]) -H3 = SparsePauliOp.from_list([("ZI", 1), ("ZZ", 1)]) - -theta1 = [0, 1, 1, 2, 3, 5] -theta2 = [0, 1, 1, 2, 3, 5, 8, 13] -theta3 = [1, 2, 3, 4, 5, 6] - -estimator = EstimatorV2() - -# calculate [ [, -# ], -# [] ] -job = estimator.run( - [ - (psi1, [H1, H3], [theta1, theta3]), - (psi2, H2, theta2) - ], - precision=0.01 -) -result = job.result() -print(f"expectation values : psi1 = {result[0].data.evs}, psi2 = {result[1].data.evs}") - -# -------------------------- -# Simulating using sampler -# -------------------------- -from qiskit_aer.primitives import SamplerV2 -from qiskit import QuantumCircuit - -# create a Bell circuit -bell = QuantumCircuit(2) -bell.h(0) -bell.cx(0, 1) -bell.measure_all() - -# create two parameterized circuits -pqc = RealAmplitudes(num_qubits=2, reps=2) -pqc.measure_all() -pqc = transpile(pqc, sim, optimization_level=0) -pqc2 = RealAmplitudes(num_qubits=2, reps=3) -pqc2.measure_all() -pqc2 = transpile(pqc2, sim, optimization_level=0) - -theta1 = [0, 1, 1, 2, 3, 5] -theta2 = [0, 1, 2, 3, 4, 5, 6, 7] - -# initialization of the sampler -sampler = SamplerV2() - -# collect 128 shots from the Bell circuit -job = sampler.run([bell], shots=128) -job_result = job.result() -print(f"counts for Bell circuit : {job_result[0].data.meas.get_counts()}") - -# run a sampler job on the parameterized circuits -job2 = sampler.run([(pqc, theta1), (pqc2, theta2)]) -job_result = job2.result() -print(f"counts for parameterized circuit : {job_result[0].data.meas.get_counts()}") +from qiskit_ibm_runtime import QiskitRuntimeService + +# Generate 3-qubit GHZ state +circ = qiskit.QuantumCircuit(3) +circ.h(0) +circ.cx(0, 1) +circ.cx(1, 2) +circ.measure_all() + +# Construct an ideal simulator +aersim = AerSimulator() + +# Perform an ideal simulation +result_ideal = aersim.run(circ).result() +counts_ideal = result_ideal.get_counts(0) +print('Counts(ideal):', counts_ideal) +# Counts(ideal): {'000': 493, '111': 531} + +# Construct a simulator using a noise model +# from a real backend. +provider = QiskitRuntimeService() +backend = provider.get_backend("ibm_kyoto") +aersim_backend = AerSimulator.from_backend(backend) + +# Perform noisy simulation +result_noise = aersim_backend.run(circ).result() +counts_noise = result_noise.get_counts(0) + +print('Counts(noise):', counts_noise) +# Counts(noise): {'101': 16, '110': 48, '100': 7, '001': 31, '010': 7, '000': 464, '011': 15, '111': 436} ``` ## Contribution Guidelines