1
$\begingroup$

Starting with a 2 qubit (each initialized as $|00\rangle$) I need to apply an $R_y(\theta)$ gate to get the following probabilistics:

  • 24% $|00\rangle$
  • 56% $|01\rangle$
  • 6% $|10\rangle$
  • 14% $|11\rangle$

I did the following, however, I am not getting exactly what is being asked for, an anyone help me and tell me where am I wrong?

from math import sqrt, acos, asin
from qiskit import QuantumCircuit, Aer, transpile
from qiskit.visualization import plot_histogram

prob = [0.24, 0.56, 0.06, 0.14]

qc = QuantumCircuit(2)

# Initialize both with state |00>
qc.initialize([1,0], 0)
qc.initialize([1,0], 1)

qc.ry(2 * acos(sqrt(prob[0])), 0)
qc.ry(2 * asin(sqrt(prob[2])), 1)

qc.measure_all()
display(qc.draw('mpl'))

backend = Aer.get_backend('aer_simulator') 

job_sim = backend.run(transpile(qc, backend), shots=1024) 
result = job_sim.result()     
plot_histogram(result.get_counts())

results

$\endgroup$
1
  • $\begingroup$ Have you tried increasing the amount of shots? $\endgroup$ Jun 12 at 19:22

1 Answer 1

0
$\begingroup$

this is how you do it without using vqe or qml, of course, you also can increase shots for accuracy.

from math import sqrt, acos, asin
import numpy as np
from qiskit import QuantumCircuit, Aer, transpile
from qiskit.visualization import plot_histogram

prob = np.sqrt([0.24, 0.56, 0.06, 0.14])

qc = QuantumCircuit(2)

# Initialize both with state |00>
# current initialize gate got reset in it, so is better to just use reset.
#qc.reset([0,1])

initial_state = prob/np.linalg.norm(prob)
qc.initialize(initial_state, [0,1])  

qc.measure_all()
display(transpile(qc, basis_gates=['ry','cx']).draw('mpl'))

backend = Aer.get_backend('aer_simulator') 

job_sim = backend.run(transpile(qc,basis_gates=['ry','cx']), shots=1024) 
result = job_sim.result()     
plot_histogram(result.get_counts())

after transpile:(you can't do basis_gates=['ry'])
enter image description here
you can do this to extract the needed ry params out

from qiskit.converters import circuit_to_dag
dag = circuit_to_dag(transpile(qc, basis_gates=['ry','cx']))
gates_param = []
for node in dag.op_nodes():
    gates_param.append(node.op.params)
for i in range(qc.num_qubits):
    print(gates_param[i][0])
qc.ry(1.98, 0)
qc.ry(0.927, 1)

enter image description here
1024 shots:
enter image description here
200000 shots:
enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks!! One more thing. If I would have to do the math manually to get the values of each angle of the Ry(theta) gates I am not reaching the same result only with the Ry(theta) and replacing with [1 0] and the prob list. Can you explain me how you reached that result? $\endgroup$
    – gattes
    Jun 12 at 21:25
  • $\begingroup$ @gattes I think it is possible manually, but I don't understand how transpile or decompose work details and I am not good at math. I suggest search up 'quantum computing numpy', try to inverse the math function. $\endgroup$
    – poig
    Jun 13 at 19:09
  • $\begingroup$ @gattes I found a detailed explanation how to do it. towardsdatascience.com/… $\endgroup$
    – poig
    20 hours ago
  • $\begingroup$ @gattes accept answer will help alot $\endgroup$
    – poig
    19 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.