<jupyter_start><jupyter_text>Addition through Fourier transform with control bit<jupyter_code>def CRz(i,t,phi,o):
    return o.Rz(t,phi/2.).CNOT(i,t).Rz(t,-phi/2.).Rz(i,phi/2.).CNOT(i,t)

def add(c,y,op,xbits):
    op=cqft(c,op,mask=xbits)
    N=len(xbits)
    for i in range(N):
        op=CRz(c,xbits[i],2.*np.pi*y/2.**(N-i),op)
    op=cqft(c,op,inverse=True,mask=xbits)
    return op

st=sqc.state(6,basis=["|%d>|%d>" % (i % 2**5,i//2**5) for i in range(2**6)])

xbits=[0,1,2,3,4]
cbit=5

st=sqc.operator(6).H(cbit)*st
print("Initial = 0\n",st)

st=add(cbit,3,sqc.operator(6),xbits=xbits)*st
print("Initial + 3\n",st)
st=add(cbit,7,sqc.operator(6),xbits=xbits)*st
print("Initial + 3 + 7\n",st)<jupyter_output>Initial = 0
    0.707107 * |0>|0>
 + 0.707107 * |0>|1>
Initial + 3
    0.707107 * |0>|0>
 + 0.707107 * |3>|1>
Initial + 3 + 7
    0.707107 * |0>|0>
 + 0.707107 * |10>|1><jupyter_text>Precompute ci<jupyter_code>def bits(y,N):
    return [ (y//2**j) % 2 for j in range(N) ]

def ci(x,n,N):
    b=bits(x,N)
    return [ sum([ b[j]*2**(i+j) for j in range(N) ]) % n for i in range(N) ]


print(ci(5,21,5))<jupyter_output>[5, 10, 20, 19, 17]<jupyter_text>Multiplication by constant (without mod n)<jupyter_code>def mult(x,op,xbits,tbits):
    Nbits=len(xbits)
    c=ci(x,2**Nbits,Nbits)
    for i in range(Nbits):
        op=add(xbits[i],c[i],op,tbits)
    return op

st=sqc.state(6,basis=["|%d>|%d>" % (i % 2**3,i//2**3) for i in range(2**6)])

xbits=[0,1,2]
tbits=[3,4,5]

st=sqc.operator(6).X(1)*st
print("Initial state\n",st)

st=mult(3,sqc.operator(6),xbits=xbits,tbits=tbits)*st
print("Mult by 3\n",st)<jupyter_output>Initial state
    1 * |2>|0>
Mult by 3
    1 * |2>|6>
{'fraction_non_alphanumeric': 0.23812351543942992, 'fraction_numerical': 0.07600950118764846, 'mean_word_length': 4.561056105610561, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 30, 'https://': 0, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '24466', 'license': 'MIT', 'n_tokens_mistral': 911, 'n_tokens_neox': 851, 'n_words': 127, 'path': 'examples/chapter1/multiply-with-constant.ipynb', 'repo_name': 'gnrraphi/sqc', 'size': '1684'}