#initialization import matplotlib.pyplot as plt import math import numpy as np # importing Qiskit from qiskit import ClassicalRegister, QuantumRegister from qiskit.providers.ibmq import least_busy from qiskit.quantum_info import Statevector from qiskit.providers.aer import QasmSimulator # import basic plot tools from qiskit.visualization import plot_histogram def initialize_s(qc,qubits): """Apply a H-gate to 'qubits' in qc""" for q in qubits: qc.h(q) return qc def numberofiterations(nqubits): temp = 2 ** nqubits; squareRoot = math.sqrt(temp) iterations = round(squareRoot) return iterations def oracle(nqubits): q = QuantumRegister(nqubits) qc = QuantumCircuit(q) #set the oracle's 0 bits qc.x(q[1]) qc.x(q[2]) #qc.x(q[5]) #qc.x(q[7]) #qc.x(q[8]) #qc.x(q[12]) #qc.x(q[13]) #qc.x(q[15]) #qc.x(q[19]) #qc.x(q[20]) #qc.x(q[21]) #qc.x(q[22]) #qc.x(q[24]) qc.h(q[nqubits-1]) #[0,3,4,6,9,10,11,14,16,17,18,23,26,27],30,ancilla_qubits=None, mode='noancilla') #qc.mcx([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],q[25],ancilla_qubits=None, mode='noancilla') #qc.cx([0],[1]) qc.mcx([0,1,2],q[nqubits-1],ancilla_qubits=None, mode='noancilla') qc.x(q[1]) qc.x(q[2]) #qc.x(q[5]) #qc.x(q[7]) #qc.x(q[8]) #qc.x(q[12]) #qc.x(q[13]) #qc.x(q[15]) #qc.x(q[19]) #qc.x(q[20]) #qc.x(q[21]) #qc.x(q[22]) #qc.x(q[24]) qc.h(q[nqubits-1]) #return the oracle as a gate. oracle = qc.to_gate() oracle.name = "U$_\omega$" return oracle def diffuser(nqubits): qc = QuantumCircuit(nqubits) # Apply transformation |s> -> |00..0> (H-gates) for qubit in range(nqubits): qc.h(qubit) # Apply transformation |00..0> -> |11..1> (X-gates) for qubit in range(nqubits): qc.x(qubit) # Do multi-controlled-Z gate qc.h(nqubits-1) qc.mcx(list(range(nqubits-1)), nqubits-1) # multi-controlled-toffoli #qc.cx([0],[1]) qc.h(nqubits-1) # Apply transformation |11..1> -> |00..0> for qubit in range(nqubits): qc.x(qubit) # Apply transformation |00..0> -> |s> for qubit in range(nqubits): qc.h(qubit) # We will return the diffuser as a gate U_s = qc.to_gate() U_s.name = "$U_s$" return U_s n = 4 qr = QuantumRegister(n-1, 'register') anc = QuantumRegister(1, 'ancilla') cr = ClassicalRegister(n-1, 'classical') grover_circuit = QuantumCircuit(qr,anc) grover_circuit = initialize_s(grover_circuit, list(range(n))) iterations = numberofiterations(n-1) for j in range(iterations ): grover_circuit.append(oracle(n), list(range(n))) grover_circuit.append(diffuser(n), list(range(n))) measure_circuit = QuantumCircuit(qr,cr) measure_circuit.measure(qr,cr) ``` length Length / transpilation issues with Grover's algorithm
#initialization import matplotlib.pyplot as plt import math import numpy as np # importing Qiskit from qiskit import ClassicalRegister, QuantumRegister from qiskit.providers.ibmq import least_busy from qiskit.quantum_info import Statevector from qiskit.providers.aer import QasmSimulator # import basic plot tools from qiskit.visualization import plot_histogram def initialize_s(qc,qubits): """Apply a H-gate to 'qubits' in qc""" for q in qubits: qc.h(q) return qc def numberofiterations(nqubits): temp = 2 ** nqubits; squareRoot = math.sqrt(temp) iterations = round(squareRoot) return iterations def oracle(nqubits): q = QuantumRegister(nqubits) qc = QuantumCircuit(q) #set the oracle's 0 bits qc.x(q[1]) qc.x(q[2]) #qc.x(q[5]) #qc.x(q[7]) #qc.x(q[8]) #qc.x(q[12]) #qc.x(q[13]) #qc.x(q[15]) #qc.x(q[19]) #qc.x(q[20]) #qc.x(q[21]) #qc.x(q[22]) #qc.x(q[24]) qc.h(q[nqubits-1]) #[0,3,4,6,9,10,11,14,16,17,18,23,26,27],30,ancilla_qubits=None, mode='noancilla') #qc.mcx([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],q[25],ancilla_qubits=None, mode='noancilla') #qc.cx([0],[1]) qc.mcx([0,1,2],q[nqubits-1],ancilla_qubits=None, mode='noancilla') qc.x(q[1]) qc.x(q[2]) #qc.x(q[5]) #qc.x(q[7]) #qc.x(q[8]) #qc.x(q[12]) #qc.x(q[13]) #qc.x(q[15]) #qc.x(q[19]) #qc.x(q[20]) #qc.x(q[21]) #qc.x(q[22]) #qc.x(q[24]) qc.h(q[nqubits-1]) #return the oracle as a gate. oracle = qc.to_gate() oracle.name = "U$_\omega$" return oracle def diffuser(nqubits): qc = QuantumCircuit(nqubits) # Apply transformation |s> -> |00..0> (H-gates) for qubit in range(nqubits): qc.h(qubit) # Apply transformation |00..0> -> |11..1> (X-gates) for qubit in range(nqubits): qc.x(qubit) # Do multi-controlled-Z gate qc.h(nqubits-1) qc.mcx(list(range(nqubits-1)), nqubits-1) # multi-controlled-toffoli #qc.cx([0],[1]) qc.h(nqubits-1) # Apply transformation |11..1> -> |00..0> for qubit in range(nqubits): qc.x(qubit) # Apply transformation |00..0> -> |s> for qubit in range(nqubits): qc.h(qubit) # We will return the diffuser as a gate U_s = qc.to_gate() U_s.name = "$U_s$" return U_s n = 4 qr = QuantumRegister(n-1, 'register') anc = QuantumRegister(1, 'ancilla') cr = ClassicalRegister(n-1, 'classical') grover_circuit = QuantumCircuit(qr,anc) grover_circuit = initialize_s(grover_circuit, list(range(n))) iterations = numberofiterations(n-1) for j in range(iterations ): grover_circuit.append(oracle(n), list(range(n))) grover_circuit.append(diffuser(n), list(range(n))) measure_circuit = QuantumCircuit(qr,cr) measure_circuit.measure(qr,cr) ``` Loading