Skip to content
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

Properly substitute symbolic parameters #152

Merged
merged 3 commits into from
Feb 12, 2024
Merged

Properly substitute symbolic parameters #152

merged 3 commits into from
Feb 12, 2024

Conversation

speller26
Copy link
Collaborator

Summary

to_qiskit now substitutes the parameter in a Qiskit gate's parameter expression instead of replacing the entire expression wholesale.

Details and comments

@speller26 speller26 linked an issue Feb 11, 2024 that may be closed by this pull request
new_gate_params = []
for param_expression, value in zip(gate_instance.params, gate_params):
# Assumes that each Qiskit gate has one free parameter per expression
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be more accurate, a gate can have many parameters (gate_instance.params), but these expressions contains a single Parameter. Here we are only looking at the gates in _GATE_NAME_TO_QISKIT_GATE where it is the case (at worst gate parameters are proportial to the used Parameter, e.g GPi). It is very unlikely that we will define a gate in this dict as Gate(Parameter("a")+Parameter("b")).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's what I was trying to say with "per expression"; it felt weird to say "each gate's parameter expressions" because the expressions themselves are referred to as the gate's parameters.

gate_cls = gate_instance.__class__
else:
raise TypeError(f'Braket gate "{gate_name}" not supported in Qiskit')
assigned = param_expression.assign(param, value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assign is the function I was looking for. So glad you bring this up. This is much better that the fix I was implementing on my side.
Side-effect, it works when passing a ParameterExpression

param_uuids = {
param.name: param._uuid for param in qiskit_circuit.parameters
}
params_qiskit = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just passing qiskit_circuit.parameters? We could assert that qiskit.parameters names are the same as those in params if we want to check the parameters.

expected_qiskit_circuit = expected_qiskit_circuit.assign_parameters(
dict(
zip(
# Need to use gate parameters because
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be simpler if we use qiskit_circuit.parameters?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is qiskit_circuit.parameters orders the parameters alphabetically, and the parameters in the _GATE_NAME_TO_BRAKET_GATE translations aren't. For example, the order of u parameters is theta, phi, lambda, but the alphabetical order is theta, lambda, phi.


expected_qiskit_circuit = QuantumCircuit(op.qubit_count)
qiskit_gate = _GATE_NAME_TO_QISKIT_GATE.get(gate_name.lower())
expected_qiskit_circuit.append(qiskit_gate, target)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for IonQ, I think this test would still miss the 2pi scaling factor since we are using directly the gate from the dict. We could maybe try to test the unitaries or a shot=0 sim.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make sense; the ideal way would be to compare the parametrized unitaries.

Copy link
Collaborator

@jcjaskula-aws jcjaskula-aws left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor comments but it looks good to me.

@speller26 speller26 merged commit b574d42 into main Feb 12, 2024
7 checks passed
@speller26 speller26 deleted the param branch February 12, 2024 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parameter are not properly translated with IonQ gates
2 participants