In [1]:
%load_ext dotenv
%dotenv
In [2]:
import os
from qiskit import transpile
from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2
from qiskit.visualization import plot_histogram
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
import numpy as np
Definition of th sign flip¶
$$\operatorname{SF}_{N,j} = O_j (C^NZ) O_j$$
$$O_j = \otimes_{l=0}^{N-1} (\operatorname{NOT}_l)^{1-j_l}$$
Choose $N$ and $j$. $j$ represents the elements of the computational basis.
In [3]:
def num2bin(num, n):
return np.array([int(i) for i in np.binary_repr(num, n)])
def O(q, j):
O_circ = QuantumCircuit(q, name=f"O_{''.join([str(i) for i in j])}")
for (i, v) in enumerate(j):
if v == 0:
O_circ.x(q[i])
return O_circ
def sf(q, j):
N = q.size
SF = QuantumCircuit(q, name=f"SF_{q.size}_{''.join([str(i) for i in j])}")
SF.append(O(q, j), q)
SF.barrier(q)
SF.mcrz(np.pi, q[:N-1], q[N-1])
SF.barrier(q)
SF.append(O(q, j), q)
return SF
In [4]:
def U_i(q, input_vector):
u_i = QuantumCircuit(q, name=f"U^i_{''.join([str(i) for i in input_vector])}")
N = q.size
u_i.h(q)
for (i, v) in enumerate(input_vector):
if v:
bin_num = num2bin(i, N)
u_i.append(sf(q, bin_num), q)
return u_i
In [5]:
def U_w(q, weight_vector):
u_w = QuantumCircuit(q, name=f"U^w_{''.join([str(i) for i in weight_vector])}")
N = q.size
for (i, v) in enumerate(weight_vector):
if v:
bin_num = num2bin(i, N)
u_w.append(sf(q, bin_num), q)
u_w.h(q)
u_w.x(q)
return u_w
In [6]:
N = 2
q = QuantumRegister(N, 'q')
input_vector = [0, 0, 0, 0]
circ = QuantumCircuit(q)
circ.append(U_i(q, input_vector), q)
circ.decompose(reps=1).draw(output='mpl')
Out[6]:
In [7]:
N = 2
q = QuantumRegister(N, 'q')
a = QuantumRegister(1, 'a')
b = ClassicalRegister(1, 'b')
circ = QuantumCircuit(q, a, b)
circ.append(U_i(q, num2bin(0, N)[::-1]), q)
circ.append(U_w(q, num2bin(0, N)), q)
circ.mcrx(np.pi, q, a[0])
circ.measure(a, b)
circ.decompose(reps=1).draw(output='mpl')
Out[7]:
In [8]:
token = os.environ["IBMQ_TOKEN"]
In [9]:
service = QiskitRuntimeService(
token=token,
channel="ibm_quantum",
)
backend = service.least_busy(operational=True, simulator=False)
/tmp/ipykernel_17005/1493915476.py:1: DeprecationWarning: The "ibm_quantum" channel option is deprecated and will be sunset on 1 July. After this date, ibm_cloud will be the only valid channel. For information on migrating to the new IBM Quantum Platform on the "ibm_cloud" channel, review the migration guide https://quantum.cloud.ibm.com/docs/migration-guides/classic-iqp-to-cloud-iqp . service = QiskitRuntimeService(
In [10]:
transpiled = transpile(circ, backend=backend)
sampler = SamplerV2(backend)
job = sampler.run([transpiled])
result = job.result()
counts = result[0].data.b.get_counts()
print("\nTotal counts are:",counts)
plot_histogram(counts)
Total counts are: {'1': 3805, '0': 291}
Out[10]:
In [11]:
import mthree
mit2 = mthree.M3Mitigation(backend)
In [12]:
mit2.cals_from_system()
Out[12]:
[<RunningManJob('d0q1ea94anx0008kyy70', backend='ibm_sherbrooke', mode_id=None)>]
In [13]:
mit2.apply_correction(counts, [N+1])
Out[13]:
{'0': np.float32(-0.05580467), '1': np.float32(1.0558047)}
In [ ]: