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'.
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)
Converting integer to its Basis State
k = cirq.big_endian_int_to_bits(3, bit_count = 4) print(k)
[0, 0, 1, 1]
for i in range(len(qubits)): qubits[i] = np.pad(np.array([k[i]]), (2*len(qubits)-1,0)) print(qubits)
[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'.