Quantum Computing Asked on August 19, 2021
I would like to play with a quantum circuit local_qasm_simulator
in QISKit, but I do not want to implement a separate quantum circuit that would prepare an initial state.
The way I do it now is by falling back to NumPy. Specifically, first, I extract matrix u
from a quantum program qp
:
cname = 'circuit name'
results = qp.execute(cname, backend='local_unitary_simulator', shots=1)
data = results.get_data(cname)
u = data['unitary']
Then, I explicitly create the state I need (e.g., $|psirangle = frac{1}{2}(|00rangle + |01rangle + |10rangle – |11rangle)$):
num_qubits = 2
psi = np.ones(2**num_qubits) / 2.0
psi[3] = -psi[3]
Finally, I apply u
to psi
with NumPy:
u @ psi
The advantage of this approach is that I can explicitly obtain the state $U |psirangle$. However, I cannot use local_qasm_simulator
and the measure()
function.
So, how could I prepare an arbitrary state, and supply it to a circuit, and run a local_qasm_simulator
?
I think you can use the initialize
function as detailed at the section "Arbitrary Initialization" at this tutorial.
As an example, this tutorial explicitly shows how to initialize the three qubit state
$$ frac{i}{sqrt{16}} | 000 rangle + frac{1}{sqrt{8}} | 001 rangle + frac{1+i}{sqrt{16}} | 010 rangle + frac{1+2i}{sqrt{8}} | 101 rangle + frac{1}{sqrt{16}} | 110 rangle .$$
Which is done using the following lines of QISKit code.
import math
desired_vector = [
1 / math.sqrt(16) * complex(0, 1),
1 / math.sqrt(8) * complex(1, 0),
1 / math.sqrt(16) * complex(1, 1),
0,
0,
1 / math.sqrt(8) * complex(1, 2),
1 / math.sqrt(16) * complex(1, 0),
0]
initialize_circuit_3q = Q_program.create_circuit('initialize_circuit_3q', [qr], [cr])
initialize_circuit_3q.initialize("init", desired_vector, [qr[0],qr[1],qr[2]])
Correct answer by Rudy Raymond Harry Putra on August 19, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP