-
Notifications
You must be signed in to change notification settings - Fork 5
/
jv.py
106 lines (72 loc) · 2.7 KB
/
jv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from qiskit import IBMQ
##IBMQ.save_account(<IBMQ TOKEN here>)
# Importing standard Qiskit libraries and configuring account
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *
# Loading your IBM Q account(s)
# provider = IBMQ.load_account()
IBMQ.load_account()
# provider = IBMQ.get_provider('ibm-q-internal', 'deployed', 'default')
# initialization
import matplotlib.pyplot as plt
import numpy as np
# importing Qiskit
from qiskit import IBMQ, BasicAer
# from qiskit.providers.ibmq import least_busy
from qiskit.providers.ibmq import *
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute
# import basic plot tools
from qiskit.visualization import plot_histogram
n = 7 # number of qubits used to represent s
def wordToBV(s) :
#convert text to binary
a_byte_array = bytearray(s, "utf8")
byte_list = []
for byte in a_byte_array:
binary_representation = bin(byte)
byte_list.append(binary_representation[9-n:])
#chop off the "0b" at the beginning. can also truncate the binary to fit on a device with N qubits
#binary has 2 extra digits for "0b", so it starts at 9 for our 7 bit operation.
print(byte_list)
circuit_array = []
length = len(byte_list)
for i in range(length):
s = byte_list[i]
#do all this stuff for every letter
# We need a circuit with n qubits, plus one ancilla qubit
# Also need n classical bits to write the output to
bv_circuit = QuantumCircuit(n+1, n)
# put ancilla in state |->
bv_circuit.h(n)
bv_circuit.z(n)
# Apply Hadamard gates before querying the oracle
for i in range(n):
bv_circuit.h(i)
# Apply barrier
bv_circuit.barrier()
# Apply the inner-product oracle
s = s[::-1] # reverse s to fit qiskit's qubit ordering
for q in range(n):
if s[q] == '0':
bv_circuit.i(q)
else:
bv_circuit.cx(q, n)
# Apply barrier
bv_circuit.barrier()
#Apply Hadamard gates after querying the oracle
for i in range(n):
bv_circuit.h(i)
# Measurement
for i in range(n):
bv_circuit.measure(i, i)
circuit_array.append(bv_circuit)
return circuit_array
circuit_to_run = wordToBV('Toronto')
#run the first letter on a simulator
backend = BasicAer.get_backend('qasm_simulator')
shots = 4096
results = execute(circuit_to_run[0], backend=backend, shots=shots).result()
answer = results.get_counts()
plot_histogram(answer)