# Convert an integer to its basis state in Cirq

I am trying to implement Quantum Adder using QFT in Cirq. I previously did the same problem using Pennylane, in which I converted an integer into its Basis state using the BasisStatePreparation method in Pennylane. However, Cirq does not seem to have any method as such. It'd be of great help if any alternative can be suggested. I want to implement this - encode an integer, say 3 |0011> for 4 qubits as we do in Pennylane using the following code-

def state_preparation(basis_id, n_qubits):
# basis_id is the integer
bits = [int(x) for x in np.binary_repr(basis_id, width=n_qubits)]
return qml.BasisStatePreparation(bits, wires=range(n_qubits))


I tried encoding qubits by padding each bit in the binary representation list with zeros in the front and then passing it into the QFT function. But, I am getting this error AttributeError: 'numpy.ndarray' object has no attribute 'dimension'.

QFT code

def qft_rotations(n_qubits):
"""A circuit performs the QFT rotations on the specified qubits.

Args:
n_qubits (list): List of qubits.
"""
n = len(n_qubits)
for i in range(n):
k = 0
yield cirq.H(n_qubits[i])
for jj in range(i+1,n,1):
k = k+1
yield (cirq.CZ ** (1/(2**(k))))(n_qubits[jj], n_qubits[i])
pass

#Visually check the QFT circuit
qubits = cirq.LineQubit.range(4)
qft = cirq.Circuit(qft_rotations(qubits))
SVGCircuit(qft)



Output for four qubits

Converting integer to its Basis State

k = cirq.big_endian_int_to_bits(3, bit_count = 4)
print(k)


Output - [0, 0, 1, 1]

for i in range(len(qubits)):
print(qubits)


Output -

[array([0, 0, 0, 0, 0, 0, 0, 0]),
array([0, 0, 0, 0, 0, 0, 0, 0]),
array([0, 0, 0, 0, 0, 0, 0, 1]),
array([0, 0, 0, 0, 0, 0, 0, 1])]

qft = cirq.Circuit(qft_rotations(qubits))
SVGCircuit(qft)


And after running the code above, I am getting this error - AttributeError: 'numpy.ndarray' object has no attribute 'dimension'.

• Hello, just for information, I know that myQLM has a QFT arithmetic class well suited for this kind of problem: myqlm.github.io/qat-lang-arith.html Dec 2, 2022 at 7:47
• Hi @user12910! Thanks for sharing about myQLM, it seems to be useful. However, I want to implement it in Cirq and do not want to use any in-built function. Dec 2, 2022 at 18:06