-
-
Notifications
You must be signed in to change notification settings - Fork 50.3k
Change from qiskit import Aer to from qiskit_aer import AerSimulator #14138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 14 commits
3cd52a6
ec5b35b
28bb84f
e2d1a1f
3556a5c
035a5be
8c4089d
b8ca7b3
f73a30f
82a1477
db53c7a
b67e5c6
d9f0915
55d0ed6
cf359b1
395e384
be55b50
069d239
d60b9ca
ce2b47e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """ | ||
| Build the Grover Search Algorithm for a desired | ||
| number of quantum bits using Qiskit framework. | ||
| This experiment runs in IBM Q simulator with 10000 shots. | ||
|
|
||
| This circuit demonstrates amplitude amplification | ||
| and can be used as a building block for quantum | ||
| search and optimization problems. | ||
|
|
||
| References: | ||
| https://en.wikipedia.org/wiki/Grover%27s_algorithm | ||
| https://qiskit.org/textbook/ch-algorithms/grover.html | ||
| """ | ||
|
|
||
| import math | ||
|
|
||
| try: | ||
| import qiskit | ||
| from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute | ||
| QISKIT_AVAILABLE = True | ||
| except ModuleNotFoundError: | ||
| QISKIT_AVAILABLE = False | ||
|
|
||
|
|
||
| def grover_search(number_of_qubits: int = 2): | ||
AnshMNSoni marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: |
||
| """ | ||
| Build and simulate Grover's search algorithm. | ||
|
|
||
| The oracle marks the |11...1> state. | ||
|
|
||
| Args: | ||
| number_of_qubits (int): number of qubits | ||
|
|
||
| Returns: | ||
| qiskit.result.counts.Counts: distribution counts. | ||
|
|
||
| Raises: | ||
| TypeError: if input is not integer | ||
| ValueError: if invalid number of qubits | ||
| ModuleNotFoundError: if qiskit is not installed | ||
|
|
||
| >>> if QISKIT_AVAILABLE: # doctest: +SKIP | ||
| ... counts = grover_search(2) | ||
| ... isinstance(counts, dict) | ||
| True | ||
| """ | ||
| if not QISKIT_AVAILABLE: | ||
| raise ModuleNotFoundError( | ||
| "qiskit is required for this algorithm. " | ||
| "Install it with: pip install qiskit qiskit-aer" | ||
| ) | ||
|
|
||
| if isinstance(number_of_qubits, str): | ||
| raise TypeError("number of qubits must be an integer.") | ||
| if number_of_qubits <= 0: | ||
| raise ValueError("number of qubits must be > 0.") | ||
| if math.floor(number_of_qubits) != number_of_qubits: | ||
| raise ValueError("number of qubits must be exact integer.") | ||
| if number_of_qubits > 10: | ||
| raise ValueError("number of qubits too large to simulate (>10).") | ||
|
|
||
| # Create registers | ||
| qr = QuantumRegister(number_of_qubits, "qr") | ||
| cr = ClassicalRegister(number_of_qubits, "cr") | ||
| quantum_circuit = QuantumCircuit(qr, cr) | ||
|
|
||
| # Step 1: Initialize superposition | ||
| quantum_circuit.h(qr) | ||
|
|
||
| # -------- Oracle (mark |11...1>) -------- | ||
| quantum_circuit.h(number_of_qubits - 1) | ||
| quantum_circuit.mcx(list(range(number_of_qubits - 1)), number_of_qubits - 1) | ||
| quantum_circuit.h(number_of_qubits - 1) | ||
|
|
||
| # -------- Diffuser -------- | ||
| quantum_circuit.h(qr) | ||
| quantum_circuit.x(qr) | ||
|
|
||
| quantum_circuit.h(number_of_qubits - 1) | ||
| quantum_circuit.mcx(list(range(number_of_qubits - 1)), number_of_qubits - 1) | ||
| quantum_circuit.h(number_of_qubits - 1) | ||
|
|
||
| quantum_circuit.x(qr) | ||
| quantum_circuit.h(qr) | ||
|
|
||
| # Measure all qubits | ||
| quantum_circuit.measure(qr, cr) | ||
|
|
||
| # Run simulator with 10000 shots | ||
| backend = Aer.get_backend("qasm_simulator") | ||
| job = execute(quantum_circuit, backend, shots=10000) | ||
|
|
||
| return job.result().get_counts(quantum_circuit) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| if QISKIT_AVAILABLE: | ||
| print(f"Total count for Grover search state is: {grover_search(3)}") | ||
| else: | ||
| print("qiskit not installed. Install with: pip install qiskit qiskit-aer") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
grover_search. If the function does not return a value, please provide the type hint as:def function() -> None: