-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for parameterized qobj #485
Add support for parameterized qobj #485
Conversation
const size_t num_instr = circuit.ops.size(); | ||
for (size_t j=0; j<num_params; j++) { | ||
// Make a copy of the initial circuit | ||
Circuit param_circuit = circuit; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to copy the circuit here? You create circuit
, and then in the case that there are params, you copy the circuit, edit that copy, and then push the copy and throw out the original - why not just edit the original?
Yes you need to copy it - you could still use reserve, emplace_back, and in-place editing of the circuits in circuits but perhaps there's not much difference
// So that results aren't correlated between experiments | ||
if (seed >= 0) { | ||
const size_t num_circs = circs.size(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
circuits.reserve(num_circs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a given exp_param each parameterization instruction must have the same number of parameterization value, however the number of parameterizations can be different for each circuit in the qobj.
Not an issue, just pointing out that this is different than the current Terra API which limits all circuits in a job to run under the same parameterizations.
qobj = assemble(transpile([qc1, qc2], basis_gates=['h', 'u3'],optimization_level=0),
parameterizations=parameterizations)
There's an issue here in indexing parameterizations based on circuit gate order. There's no guarantee (even at optimization_level=0
) that gates will be in the same order when the are listed in the qobj. (Using 0 as a filler param makes this somewhat more problematic, in that if param substitutions happen on the wrong gate, the circuit will likely still be executable.)
parameterizations = [
[[[0, 0], np.linspace(0, 2*np.pi, n_params0)]],
[[[1, 2], np.linspace(0, 2*np.pi, n_params1)]]
]
Is there an extra set of []
in the example here?
throw std::invalid_argument(R"(Invalid parameterized qobj: parameterization value out of range)"); | ||
} | ||
// Update the param | ||
op.params[param_pos] = params.second[j]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does the typing work here? Do we only allow a single parameter type? Or am I missing some clever templating?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment this only works for complex or double params (basically u1, u2, u3 gates).
@kdk It looks like an extra set of brackets because there is only 1 parameterized instruction in each of the example circuits. Also yes, it is sensitive to order in the assembled qobj and assumes whomever made the parameterization configuration knew the index of each instruction in the qobj experiments lists. As you say, even with optimization level zero there is no guarantee this will be the same order as circuits (Maybe if you call assemble on the circuit without any transpiling you can guarantee this though?) |
This will be true as long as there aren't any classical conditionals (which would add instructions for the qasm to qobj conditional bfunc conversion: https://github.com/Qiskit/qiskit-terra/blob/2ee7a3a/qiskit/assembler/assemble_circuits.py#L121 ). |
0de1317
to
a361eb1
Compare
d1caf3d
to
e494d2c
Compare
* Add support for parameterized qobj * Add Parameterized Qobj tests Co-authored-by: Donny Greenberg <dongreenberg2@gmail.com>
* Add support for parameterized qobj * Add Parameterized Qobj tests Co-authored-by: Donny Greenberg <dongreenberg2@gmail.com> (cherry picked from commit 870d163)
Summary
Allows specifying a parameterized qobj which will be expanded into multiple circuits when loading the qobj JSON.
Details and comments
qobj paramterizations are specified in the
QobjConfig
as"parameterizations": [exp_0_params, ... ]
where
exp_k_params = [[[i, j], [val_0, val_1, ....]], ...]
where
i
is the instruction position in thek
qobj circuit,j
is the param position for that instruction, andval_n
is the value of the n-th parameterization.For a given exp_param each parameterization instruction must have the same number of parameterization value, however the number of parameterizations can be different for each circuit in the qobj.
If non-empty the length of
"parameterizations"
must match the number of experiments in the qobj or an invalid qobj exception will be raised (if one of the circuits shouldn't be parameterized then it'sexp_k_params
should be set to an empty list[]
).Example
returns