0
$\begingroup$

Is there any way to get the quantum gate values more precisely in qiskit?

For example, if I want to create 1/√3[|00⟩+|01⟩+|11⟩]. By using initialize() I get the following circuit by using the draw() command. enter image description here

However, I would like to know the more precise value of the first argument of R() i.e. 1.23 in R(1.23,pi/2) e.g. it could be 1.228967

Can we generate details files of angles of quantum circuits?

Here is the code for that:

import numpy as np
from qiskit import QuantumCircuit
from qiskit.transpiler.passes import Decompose

qc = QuantumCircuit(2)
initial_state = np.array([1, 1, 0, 1], dtype=float)
initial_state /= np.linalg.norm(initial_state)
qc.initialize(initial_state)
qc.decompose().decompose().decompose().decompose().draw("mpl")
$\endgroup$

3 Answers 3

3
$\begingroup$

This is probably not the only way to do it, but you can always look at the QASM file. You can do it with qc.qasm(formatted=True), which will show you the underlying QASM code. In your case, it looks like this:

OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
ry(3*pi/4) q[0];
ry(1.2309594) q[1];
cx q[1],q[0];
ry(-pi/4) q[0];
cx q[1],q[0]; 

The exact angle for Ry gate is 1.2309594.

$\endgroup$
1
$\begingroup$

Qiskit stores Instruction parameters in params attribute. So, you can access these values as follows:

for _instruction in circ.data:
    if len(_instruction[0].params) > 0:
        print('\nInstruction:', _instruction[0].name)
        print('Params:', [str(_param) for _param in _instruction[0].params])

Where circ is the decomposed circuit.

$\endgroup$
1
$\begingroup$

you can extract it directly like this, I recommend using transpile to decompose all to ry gate instead, where draw() will show decimal place without round it to 3 and keep the pi

from qiskit.converters import circuit_to_dag
dag = circuit_to_dag(transpile(qc, basis_gates=['ry','cx']))# where you can modify gate you want
gates_param = []
for node in dag.op_nodes():
    if node.op.name == 'ry': #select the gate's parameter you want to extract
        gates_param.append(node.op.params)
print(gates_param)
$\endgroup$

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.