<jupyter_start><jupyter_text>_*Quantum Fourier Transform for Machine Learning*_ *** Contributors:**ML Use Case**: 2020 - <NAME>**Template**: 2018 - Anna Phan  Overview 1) Use QFT to create informative features for a classical machine learning task.  Will some nuance be revealed? 2) Output the state vectors for us in a classical machine learning task. 3) Show that the QFT features add value.  Implementation<jupyter_code># Importing QISKit
import math
from qiskit import *
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import Aer, execute

# Import basic plotting tools
from qiskit.tools.visualization import plot_histogram
import qiskit.tools.jupyter  # This is the where the magic happens (literally).<jupyter_output><empty_output><jupyter_text>Setup API Services<jupyter_code>IBMQ.load_account();
provider = IBMQ.get_provider(group='open')
provider.backends()
for backend in provider.backends():
    print( backend.status() )
#set backend

backend = provider.get_backend('ibmq_essex')
#backend = provider.get_backend('ibmqx2')
#backend = Aer.get_backend('ibmq_qasm_simulator')
#backend = provider.get_backend('ibmqx2')
#backend = Aer.get_backend('qasm_simulator')

#Run the quantum circuit on a statevector simulator backend
backend2 = Aer.get_backend('statevector_simulator')<jupyter_output><empty_output><jupyter_text>First let's define the QFT function, as well as a function that creates a state from which a QFT will return 1:<jupyter_code>#circuit.initialize(desired_vector, [q[0],q[1],q[2]])
def input_state(circ, q, n):
    """n-qubit input state for QFT that produces output 1."""
    for j in range(n):
        circ.h(j)
        circ.u1(math.pi/float(2**(j)), j).inverse()
def qft(circ, q, n):
    """n-qubit QFT on q in circ."""
    for j in range(n):
        for k in range(j):
            circ.cu1(math.pi/float(2**(j-k)), j, k)
        circ.h(j)<jupyter_output><empty_output><jupyter_text>Let's now implement a QFT on a prepared three qubit input state that should return $001$:  Using custom functions<jupyter_code>#create circuit
set_size = 12
q = QuantumRegister(set_size)
c = ClassicalRegister(set_size)
qc = QuantumCircuit(q, c)
#input
input_state(qc, q, set_size)
#QFT
qft(qc, q, set_size)
#measurement
for i in range(set_size):
    qc.measure(q[i], c[i])

print(qc.qasm())
qc.draw()<jupyter_output><empty_output><jupyter_text>There are 2 circuits:  qc and nc.  The nc circuit is the transpiled one.<jupyter_code>#transpile: set couplings based on the configration of the backend
#new_circuits = transpile(qc, backend, optimization_level=1)

#nc = transpile(qc, backend, optimization_level=1)
nc.draw()<jupyter_output><empty_output><jupyter_text>Execute Jobs<jupyter_code>circ = qc
#state vectors
#job = execute(circ, backend, shots=1024)
job = execute(circ, backend2)
result = job.result()

print("QFT Results: ", result)
#print(result.get_counts(circ))
#ONLY for state vector simulation
outputstate = result.get_statevector(circ, decimals=3)
print(outputstate)
#ONLY for state vector simulation
from qiskit.visualization import plot_state_city
plot_state_city(outputstate)
#real QPU
#job_sim = execute(qc, backend)
job = execute(qc, backend, shots=1024)
result = job.result()

print("QFT Results: ", result)
print(result.get_counts(qc))
counts = result.get_counts(qc)
print(counts)
#Real QPU - After transpiling
job = execute(nc, backend, shots=1024)
result = job.result()

print("QFT Results: ", result)
print(result.get_counts(nc))
counts2 = result.get_counts(nc)
print(counts2)
#job_sim2 = execute(["qft3"], backend, coupling_map=ibmqx2_coupling, shots=1024, max_credits=3, wait=10, timeout=240)
#plot_histogram(counts)
plot_histogram(counts)
plot_histogram(counts2)
qc.width()
qc.n_qubits
qc.count_ops()
# We can also get just the raw count of operations by computing the circuits size:
qc.size()<jupyter_output><empty_output><jupyter_text>Notes and Tests<jupyter_code>#QFT circuit
circ = QuantumCircuit(3)
circ.h(0)
circ.u1(-3.141592653589793,0)
circ.h(1)
circ.u1(-1.570796326794897, 1)
circ.h(2)
circ.u1(-0.785398163397448,2)
circ.h(0)
circ.cu1(1.570796326794897, 1,0)
circ.h(1)
circ.cu1(0.785398163397448,2,0)
circ.cu1(1.570796326794897,2,1)
circ.h(1)

meas = QuantumCircuit(3, 3)
meas.measure(range(3),range(3))
qc = circ+meas
from qiskit.visualization import plot_histogram
plot_histogram(counts)
#input state on the QFT
input_state(qft3, q, 3)
#transformation
qft(qft3, q, 3)
#measurement
for i in range(3):
    qft3.measure(q[i], c[i])
print(qft3.qasm())
simulate = Q_program.execute(["qft3"], backend="local_qasm_simulator", shots=1024)
simulate.get_counts("qft3")
#redo this in more current Qiskit version language
q = Q_program.create_quantum_register("q", 3)
c = Q_program.create_classical_register("c", 3)
qft3 = Q_program.create_circuit("qft3", [q], [c])

input_state(qft3, q, 3)
qft(qft3, q, 3)

for i in range(3):
    qft3.measure(q[i], c[i])
print(qft3.qasm())

simulate = Q_program.execute(["qft3"], backend="local_qasm_simulator", shots=1024)
simulate.get_counts("qft3")<jupyter_output><empty_output><jupyter_text>We indeed see that the outcome is always $001$ when we execute the code on the simulator.Note that as written, it is not possible to run the code on either `ibmqx2` or `ibmqx3`, as the qubit couplings used don't exist. So we'll need to get the `ibmqx2` coupling map and use that.<jupyter_code>ibmqx2_backend = Q_program.get_backend_configuration('ibmqx2')
ibmqx2_coupling = ibmqx2_backend['coupling_map']

run = Q_program.execute(["qft3"], backend="ibmqx2", coupling_map=ibmqx2_coupling, shots=1024, max_credits=3, wait=10, timeout=240)
plot_histogram(run.get_counts("qft3"))<jupyter_output><empty_output><jupyter_text>We see that the highest probability outcome $(00)001$ when we execute the code on `ibmqx2`.<jupyter_code>%run "../version.ipynb"<jupyter_output><empty_output><jupyter_text>Backup Code<jupyter_code>#https://qiskit-staging.mybluemix.net/documentation/release_notes.html?highlight=quantumprogram

from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import Aer, execute

q = QuantumRegister(2)
c = ClassicalRegister(2)
qc = QuantumCircuit(q, c)

qc.h(q[0])
qc.cx(q[0], q[1])
qc.measure(q, c)

backend = get_backend('qasm_simulator')

job_sim = execute(qc, backend)
sim_result = job_sim.result()

print("simulation: ", sim_result)
print(sim_result.get_counts(qc))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12616532007458048, 'fraction_numerical': 0.03853325046612803, 'mean_word_length': 6.644893111638955, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 1, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '323528', 'license': 'Apache-2.0', 'n_tokens_mistral': 2536, 'n_tokens_neox': 2326, 'n_words': 569, 'path': 'Nicole/fourier_transform_NCB.ipynb', 'repo_name': 'NickyBar/QIP', 'size': '6445'}