You are on page 1of 3

1 http://localhost:8888/nbconvert/html/Desktop/quiskit/1.ipynb?downlo...

In [55]: from qiskit import QuantumCircuit


from qiskit.quantum_info import Statevector, Operator
from tools import array_to_latex
import numpy as np

In [41]: #vec = np.zeros(5)


vec = []
vec.append(1+1*1.j)
vec.append((1/np.sqrt(2))+0.1*1.j)
sv = Statevector(vec, (2,1))
sv

Out[41]: Statevector([1. +1.j , 0.70710678+0.1j],


dims=(2, 1))

In [42]: print(array_to_latex(sv.data))

None

In [54]: vec1 = [0.+0.j for i in range(5)]


vec1[0] = (1/np.sqrt(2))+(1/np.sqrt(2))*1.j
sv1 = Statevector(vec1, (5,1))
print(array_to_latex(sv1.data))

None

In [56]: def phase_oracle(n, indices_to_mark, name="Oracle"):


qc = QuantumCircuit(n, name=name)
oracle_matrix = np.identity(2**n)
for index_to_mark in indices_to_mark:
oracle_matrix[index_to_mark, index_to_mark] = -1
qc.unitary(Operator(oracle_matrix), range(n))
return qc

1 de 3 23/11/2020 18:17
1 http://localhost:8888/nbconvert/html/Desktop/quiskit/1.ipynb?downlo...

In [57]: n = 5
x = np.random.randint(2**n)
marked = [x]
qc = phase_oracle(n, marked)
qc.draw()

Out[57]: ┌──────────┐
q_0: ┤0 ├
│ │
q_1: ┤1 ├
│ │
q_2: ┤2 unitary ├
│ │
q_3: ┤3 ├
│ │
q_4: ┤4 ├
└──────────┘

In [58]: x

Out[58]: 11

In [185]: vs = np.zeros(5, dtype=complex)


vs = Statevector(vs, (5,1))
print(array_to_latex(vs.data))

None

In [ ]:

In [186]: vec2 = [0 + 0.j for i in range(5)]


vec2[0] = 1 + 0.j
vec2[1] = 1
vec2[3] = 1
sv2 = Statevector(vec2, (5,1))
print(array_to_latex(sv2.data))

None

In [227]: sv = Statevector.from_int(11,(32))
sv.num_qubits

Out[227]: 5

2 de 3 23/11/2020 18:17
1 http://localhost:8888/nbconvert/html/Desktop/quiskit/1.ipynb?downlo...

In [228]: sv.data

Out[228]: array([0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,


0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.
j])

In [229]: sv.num_qubits

Out[229]: 5

In [230]: nsv = sv.evolve(qc)

In [231]: nsv.data

Out[231]: array([ 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,


0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, -1.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j])

In [100]: isinstance(sv, Statevector)

Out[100]: True

In [ ]:

3 de 3 23/11/2020 18:17

You might also like