<jupyter_start><jupyter_text>Grover - example with 2 qubits<jupyter_code>#Just run once to import all needed classes.
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
from math import pi
#Methods that will be used in the Oracle.
def controlled_anti_z(circuit, q0, q1):
    circuit.h(q1)
    circuit.cx(q0,q1)
    circuit.h(q1)
    circuit.cx(q0,q1)
    circuit.h(q1)
    circuit.cx(q0,q1)
    circuit.h(q1)
    circuit.cx(q0,q1)
    circuit.h(q1)
    circuit.cx(q0,q1)
    circuit.h(q1)

def controlled_z(circuit, q0, q1):
    circuit.h(q1)
    circuit.cx(q0,q1)
    circuit.h(q1)<jupyter_output><empty_output><jupyter_text>Oracle (for query)<jupyter_code>def oracle(circuit,quantum,number):
    if(number%4 < 2):
        circuit.x(quantum[1])
    if(number%2 == 0):
        controlled_anti_z(circuit,quantum[1],quantum[0])
    else:
        controlled_z(circuit,quantum[1],quantum[0])
    if(number%4 < 2):
        circuit.x(quantum[1])<jupyter_output><empty_output><jupyter_text>Example of the Oracle.<jupyter_code>qreg1 =  QuantumRegister(2)
creg1 = ClassicalRegister(2)

mycircuit1 = QuantumCircuit(qreg1,creg1)

#Any value between 0 and 3.
oracle(mycircuit1,qreg1,2)

job = execute(mycircuit1,Aer.get_backend('unitary_simulator'))
u=job.result().get_unitary(mycircuit1,decimals=3)
for i in range(len(u)):
    s=""
    for j in range(len(u)):
        val = str(u[i][j].real)
        while(len(val)<6): val  = " "+val
        s = s + val
    print(s)

mycircuit1.draw(output='mpl')<jupyter_output><empty_output><jupyter_text>Diffusion (inversion)<jupyter_code>def diffusion(circuit,quantum):
    circuit.x(quantum[2])
    circuit.h(quantum[2])
    
    circuit.h(quantum[1])
    circuit.h(quantum[0])
    circuit.x(quantum[1])
    circuit.x(quantum[0])

    circuit.ccx(quantum[1],quantum[0],quantum[2])

    circuit.x(quantum[1])
    circuit.x(quantum[0])
    circuit.h(quantum[1])
    circuit.h(quantum[0])

    circuit.h(quantum[2])
    circuit.x(quantum[2])<jupyter_output><empty_output><jupyter_text>Demonstration of the diffusion.<jupyter_code>qreg2 =  QuantumRegister(3)
creg2 = ClassicalRegister(3)

mycircuit2 = QuantumCircuit(qreg2,creg2)

diffusion(mycircuit2,qreg2)

job = execute(mycircuit2,Aer.get_backend('unitary_simulator'))
u=job.result().get_unitary(mycircuit2,decimals=3)
for i in range(len(u)):
    s=""
    for j in range(len(u)):
        val = str(u[i][j].real)
        while(len(val)<6): val  = " "+val
        s = s + val
    print(s)

mycircuit1.draw(output='mpl')<jupyter_output><empty_output><jupyter_text>Grover - demo<jupyter_code>qreg3 =  QuantumRegister(3)
creg3 = ClassicalRegister(3)

mycircuit3 = QuantumCircuit(qreg3,creg3)

#Grover itself
#Initial step - superposition.
for i in range(2):
    mycircuit3.h(qreg3[i])
#Grover iterations.
for i in range(1):
    mycircuit3.barrier()
    oracle(mycircuit3,qreg3,0)
    oracle(mycircuit3,qreg3,1)
    oracle(mycircuit3,qreg3,2)
    mycircuit3.barrier()
    diffusion(mycircuit3,qreg3)

mycircuit3.measure(qreg3,creg3)

job = execute(mycircuit3,Aer.get_backend('qasm_simulator'),shots=10000)
counts1 = job.result().get_counts(mycircuit3)
print(counts1) # print the outcomes

mycircuit3.draw(output='mpl')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14540424223793422, 'fraction_numerical': 0.036889025514909315, 'mean_word_length': 4.821109123434705, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '78569', 'license': 'Apache-2.0', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1269, 'n_words': 189, 'path': 'bronze/.ipynb_checkpoints/Grover for QDrive-checkpoint.ipynb', 'repo_name': 'dilyaraahmetshina/quantum_computings', 'size': '3253'}