Skip to content

Instantly share code, notes, and snippets.

@instance01
Last active February 6, 2021 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save instance01/e5e41a7f3ec6d2d00939c851d532e446 to your computer and use it in GitHub Desktop.
Save instance01/e5e41a7f3ec6d2d00939c851d532e446 to your computer and use it in GitHub Desktop.
import qiskit as qk
simulator = qk.BasicAer.get_backend('statevector_simulator')
q = qk.QuantumRegister(3)
c = qk.ClassicalRegister(3)
total_sum = None
for i in range(8):
arr = [0, 0, 0, 0, 0, 0, 0, 0]
arr[i] = 1
print(arr)
circuit = qk.QuantumCircuit(q, c)
circuit.initialize(arr, [q[0], q[1], q[2]])
circuit.h(q[0])
circuit.h(q[1])
circuit.h(q[2])
print(circuit)
job = qk.execute(circuit, simulator)
result = job.result()
ket = job.result().get_statevector()
if total_sum is None:
total_sum = ket
else:
for j, amplitude in enumerate(ket):
# From Qiskits example - they marked 5 and 6.
if i == 5 or i == 6:
total_sum[j] -= amplitude
else:
total_sum[j] += amplitude
for amplitude in ket:
print(amplitude)
print('Total:')
for amplitude in total_sum:
print(round(amplitude, 5))
[1, 0, 0, 0, 0, 0, 0, 0]
┌──────────────────────────────┐┌───┐
q0_0: ┤0 ├┤ H ├
│ │├───┤
q0_1: ┤1 initialize(1,0,0,0,0,0,0,0) ├┤ H ├
│ │├───┤
q0_2: ┤2 ├┤ H ├
└──────────────────────────────┘└───┘
c0: 3/═════════════════════════════════════
(0.35355339059327384+0j)
(0.3535533905932738+0j)
(0.3535533905932738+0j)
(0.35355339059327373+0j)
(0.3535533905932738+0j)
(0.35355339059327373+0j)
(0.35355339059327373+0j)
(0.3535533905932737+0j)
[0, 1, 0, 0, 0, 0, 0, 0]
┌──────────────────────────────┐┌───┐
q0_0: ┤0 ├┤ H ├
│ │├───┤
q0_1: ┤1 initialize(0,1,0,0,0,0,0,0) ├┤ H ├
│ │├───┤
q0_2: ┤2 ├┤ H ├
└──────────────────────────────┘└───┘
c0: 3/═════════════════════════════════════
(0.35355339059327373-4.329780281177467e-17j)
(-0.35355339059327395+4.3297802811774677e-17j)
(0.3535533905932737-4.329780281177466e-17j)
(-0.3535533905932738+4.329780281177467e-17j)
(0.3535533905932737-4.329780281177466e-17j)
(-0.3535533905932739+4.329780281177467e-17j)
(0.3535533905932736-4.329780281177465e-17j)
(-0.35355339059327373+4.329780281177466e-17j)
[0, 0, 1, 0, 0, 0, 0, 0]
┌──────────────────────────────┐┌───┐
q0_0: ┤0 ├┤ H ├
│ │├───┤
q0_1: ┤1 initialize(0,0,1,0,0,0,0,0) ├┤ H ├
│ │├───┤
q0_2: ┤2 ├┤ H ├
└──────────────────────────────┘└───┘
c0: 3/═════════════════════════════════════
(0.35355339059327384-4.329780281177467e-17j)
(0.35355339059327373-4.329780281177466e-17j)
(-0.3535533905932738+4.3297802811774677e-17j)
(-0.35355339059327373+4.329780281177467e-17j)
(0.3535533905932738-4.329780281177466e-17j)
(0.3535533905932737-4.329780281177465e-17j)
(-0.35355339059327373+4.329780281177467e-17j)
(-0.3535533905932737+4.329780281177466e-17j)
[0, 0, 0, 1, 0, 0, 0, 0]
┌──────────────────────────────┐┌───┐
q0_0: ┤0 ├┤ H ├
│ │├───┤
q0_1: ┤1 initialize(0,0,0,1,0,0,0,0) ├┤ H ├
│ │├───┤
q0_2: ┤2 ├┤ H ├
└──────────────────────────────┘└───┘
c0: 3/═════════════════════════════════════
(0.35355339059327373-8.659560562354928e-17j)
(-0.3535533905932736+8.659560562354932e-17j)
(-0.3535533905932736+8.659560562354932e-17j)
(0.35355339059327384-8.659560562354934e-17j)
(0.3535533905932737-8.659560562354927e-17j)
(-0.35355339059327356+8.65956056235493e-17j)
(-0.35355339059327356+8.65956056235493e-17j)
(0.3535533905932738-8.659560562354932e-17j)
[0, 0, 0, 0, 1, 0, 0, 0]
┌──────────────────────────────┐┌───┐
q0_0: ┤0 ├┤ H ├
│ │├───┤
q0_1: ┤1 initialize(0,0,0,0,1,0,0,0) ├┤ H ├
│ │├───┤
q0_2: ┤2 ├┤ H ├
└──────────────────────────────┘└───┘
c0: 3/═════════════════════════════════════
(0.35355339059327384+0j)
(0.3535533905932738+0j)
(0.3535533905932738+0j)
(0.35355339059327373+0j)
(-0.3535533905932738+4.329780281177467e-17j)
(-0.35355339059327373+4.329780281177466e-17j)
(-0.35355339059327373+4.329780281177466e-17j)
(-0.3535533905932737+4.3297802811774646e-17j)
[0, 0, 0, 0, 0, 1, 0, 0]
┌──────────────────────────────┐┌───┐
q0_0: ┤0 ├┤ H ├
│ │├───┤
q0_1: ┤1 initialize(0,0,0,0,0,1,0,0) ├┤ H ├
│ │├───┤
q0_2: ┤2 ├┤ H ├
└──────────────────────────────┘└───┘
c0: 3/═════════════════════════════════════
(0.3535533905932737-8.65956056235493e-17j)
(-0.3535533905932739+8.659560562354935e-17j)
(0.3535533905932736-8.65956056235493e-17j)
(-0.35355339059327373+8.659560562354932e-17j)
(-0.35355339059327373+8.659560562354933e-17j)
(0.35355339059327395-8.659560562354937e-17j)
(-0.3535533905932737+8.659560562354932e-17j)
(0.3535533905932738-8.659560562354934e-17j)
[0, 0, 0, 0, 0, 0, 1, 0]
┌──────────────────────────────┐┌───┐
q0_0: ┤0 ├┤ H ├
│ │├───┤
q0_1: ┤1 initialize(0,0,0,0,0,0,1,0) ├┤ H ├
│ │├───┤
q0_2: ┤2 ├┤ H ├
└──────────────────────────────┘└───┘
c0: 3/═════════════════════════════════════
(0.3535533905932738-4.329780281177467e-17j)
(0.3535533905932737-4.329780281177466e-17j)
(-0.35355339059327373+8.659560562354932e-17j)
(-0.3535533905932737+8.65956056235493e-17j)
(-0.35355339059327384+4.329780281177468e-17j)
(-0.35355339059327373+4.329780281177467e-17j)
(0.3535533905932738-8.659560562354934e-17j)
(0.35355339059327373-8.659560562354933e-17j)
[0, 0, 0, 0, 0, 0, 0, 1]
┌──────────────────────────────┐┌───┐
q0_0: ┤0 ├┤ H ├
│ │├───┤
q0_1: ┤1 initialize(0,0,0,0,0,0,0,1) ├┤ H ├
│ │├───┤
q0_2: ┤2 ├┤ H ├
└──────────────────────────────┘└───┘
c0: 3/═════════════════════════════════════
(0.3535533905932737-8.659560562354928e-17j)
(-0.35355339059327356+1.2989340843532393e-16j)
(-0.35355339059327356+1.2989340843532393e-16j)
(0.3535533905932738-8.659560562354934e-17j)
(-0.35355339059327373+8.65956056235493e-17j)
(0.3535533905932736-1.2989340843532396e-16j)
(0.3535533905932736-1.2989340843532398e-16j)
(-0.35355339059327384+8.659560562354935e-17j)
Total:
(1.41421-0j)
0j
0j
(1.41421-0j)
(1.41421-0j)
(-0+0j)
(-0+0j)
(-1.41421+0j)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment