Algoritmo di Grover (Grover algorithm)

Post:

:it: Mi è piaciuto il codice che ho creato con l’aiuto di chat gpt.

Ora ve lo mostro :wink:

:uk: “I liked the code I created with the help of ChatGPT.”

“Now I’ll show it to you.”

:it: Sarebbe bello avere un computer quantistico a casa :kissing:

:uk: “It would be nice to have a quantum computer at home.”

:it: ALGORITMO

import qiskit as qiskit

def grover(n, target):
“”"
Implementazione dell’algoritmo di Grover per il computer quantistico.

Args:
n: Dimensione dello spazio di ricerca.
target: Valore cercato.

Returns:
L’indice del valore cercato.
“”"

Inizializza il registro di qubit nello stato di Hadamard.

register = qiskit.QuantumRegister(n)
circuit = qiskit.QuantumCircuit(register)
circuit.h(register)

Applica l’oracolo della fase.

for i in range(n):
circuit.cx(register[i], register[target[i]])

Applica la porta di Grover ottimizzata.

for i in range(n):
circuit.z(register[i])
circuit.h(register[i])

Esegue l’algoritmo di Grover.

result = qiskit.execute(circuit, backend=qiskit.Aer.get_backend(“qasm_simulator”))
return result.get_counts()[0]

def main():

Cerca il numero 7 in un set di dati di 8 elementi.

n = 8
target = [0, 0, 0, 0, 1, 0, 0, 0]

Esegue l’algoritmo di Grover.

index = grover(n, target)

Stampa il risultato.

print(“Il valore cercato è:”, index)

if name == “main”:
main()

:uk: ALGORITHM

import qiskit as qiskit

def grover(n, target):
“”"
Implementation of the Grover algorithm for a quantum computer.

Args:
n: Dimension of the search space.
target: Target value to be found.

Returns:
The index of the target value.
“”"

Initialize the qubit register in a Hadamard state.

register = qiskit.QuantumRegister(n)
circuit = qiskit.QuantumCircuit(register)
circuit.h(register)

Apply the phase oracle.

for i in range(n):
circuit.cx(register[i], register[target[i]])

Apply the optimized Grover diffusion operator.

for i in range(n):
circuit.z(register[i])
circuit.h(register[i])

Execute the Grover algorithm.

result = qiskit.execute(circuit, backend=qiskit.Aer.get_backend(“qasm_simulator”))
return result.get_counts()[0]

def main():

Search for the number 7 in a data set of 8 elements.

n = 8
target = [0, 0, 0, 0, 1, 0, 0, 0]

Run the Grover algorithm.

index = grover(n, target)

Print the result.

print(“The target value is:”, index)

if name == “main”:
main()

:it: mi ha creato anche un codice poetico :blush:

:uk: “He has also created a poetic code for me.”

import qiskit as qiskit
import random as random

def grover(n, target):
“”"
Implementation of the Grover algorithm for a quantum computer.

Args:
n: Dimension of the search space.
target: Target value to be found.

Returns:
The index of the target value.
“”"

Initialize the qubit register in a Hadamard state.

register = qiskit.QuantumRegister(n)
circuit = qiskit.QuantumCircuit(register)
circuit.h(register)

Apply the phase oracle.

for i in range(n):
circuit.cx(register[i], register[target[i]])

Apply the optimized Grover diffusion operator.

for i in range(n):
circuit.z(register[i])
circuit.h(register[i])

Execute the Grover algorithm.

result = qiskit.execute(circuit, backend=qiskit.Aer.get_backend(“qasm_simulator”))
return result.get_memory()[0]

def poetic_code():
“”"
A poetic code that uses the Grover algorithm to generate a poem.

Returns:
A poem about quantum entanglement.
“”"

Define a list of words related to quantum research.

words = [“qubit”, “superposition”, “measurement”, “entanglement”, “coherence”, “decoherence”, “interference”, “algorithm”, “circuit”, “oracle”]

Define a list of words related to poetry.

poetry = [“love”, “dream”, “heart”, “soul”, “light”, “dark”, “life”, “death”, “joy”, “pain”]

Choose a random word from the list of words.

word = random.choice(words)

Use the Grover algorithm to find the index of the word in the list of words.

n = len(words)
target = [0] * n
target[words.index(word)] = 1
index = grover(n, target)

Print the word and the index.

print(“The word is:”, word)
print(“The index is:”, index)

Generate a poem using the word and the index as inspiration.

poem = “”

if word == “qubit”:
poem = “”“A qubit is a quantum bit
That can be zero or one or both
It can store more information than a classical bit
But it can also collapse with a single touch”“”

elif word == “superposition”:
poem = “”“Superposition is a quantum state
Where many possibilities coexist
It is like a dream that can create
Any reality that you wish”“”

elif word == “measurement”:
poem = “”“Measurement is a quantum act
That reveals the hidden truth
It is like a choice that can impact
The future that you choose”“”

elif word == “entanglement”:
poem = “”“Entanglement is a quantum bond
That connects two distant souls
It is like a love that goes beyond
The limits of space and time and roles”“”

elif word == “coherence”:
poem = “”“Coherence is a quantum feature
That preserves the quantumness
It is like a harmony that can nurture
The beauty of the oneness”“”

elif word == “decoherence”:
poem = “”“Decoherence is a quantum process
That destroys the quantum order
It is like a chaos that can oppress
The freedom of the wonder”“”

elif word == “interference”:
poem = “”“Interference is a quantum phenomenon
That combines the quantum waves
It is like a dance that can spawn
The patterns of the graves”“”

elif word == “algorithm”:
poem = “”“Algorithm is a quantum method
That solves a problem fast
It is like a logic that can lead
The way to the contrast”“”

elif word == “circuit”:
poem = “”“Circuit is a quantum device
That implements a quantum function
It is like a tool that can suffice
The need for the reduction”“”

elif word == “oracle”:
poem = “”“Oracle is a quantum entity
That knows the answer to a question
It is like a wisdom that can see
The essence of the suggestion”“”

Print the poem.

print(“The poem is:”)
print(poem)

def main():

Run the poetic code.

poetic_code()

if name == “main”:
main()