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

Enable use of qiskit ParameterExpressions for circuit conversion: qiskit to braket #139

Merged
merged 21 commits into from
Feb 12, 2024

Conversation

lukasvoss
Copy link
Contributor

@lukasvoss lukasvoss commented Jan 26, 2024

Summary

  • Users that work with a qiskit circuit that was parameterized using a qiskit ParameterExpression can now convert their qiskit circuit to a braket circuit using the convert_qiskit_to_braket_circuit function.

Details

  • A qiskit ParameterExpression could be for example a qiskit Parameter object with a constant (i.e., float) offset. This could be relevant if one uses a constant baseline for a rotation value on which a variable Parameter then gets added.

This could look something like this for a parametrized CNOT gate in qiskit:

from qiskit.circuit import QuantumCircuit, QuantumRegister
from qiskit_braket_provider.providers import adapter

q_reg = QuantumRegister(2)
my_qc = QuantumCircuit(q_reg, name="custom_cx")

baseline = np.pi * np.array([0.1, 0.3, 0.7, 0.3, -0.4, 0.4, -0.3])
params = ParameterVector(name='a', length=7)

my_qc.u(
    baseline[0] + params[0], # This 'sum' constitutes a qiskit ParameterExpression
    baseline[1] + params[1],
    baseline[2] + params[2],
    q_reg[0],
)
my_qc.u(
    baseline[3] + params[3],
    baseline[4] + params[4],
    baseline[5] + params[5],
    q_reg[1],
)
my_qc.rzx(baseline[6] + params[6], q_reg[0], q_reg[1])

braket_circuit = adapter.convert_qiskit_to_braket_circuit(my_qc)

…t to braket

This modification allows users to convert their qiskit circuit object that was parametrized with a qiskit ParameterVector to convert it to a braket circuit.
@CLAassistant
Copy link

CLAassistant commented Jan 26, 2024

CLA assistant check
All committers have signed the CLA.

@laurencap
Copy link
Collaborator

Hi @lukasvoss, thank you for the contribution! This is a great feature. Please add some tests that cover the lines you've added and show that your changes work correctly.

By default, qiskit names elements of a ``ParameterVector`` are named like this 'a[<index>]'. The '[]' parentheses are not supported by the QASM 3 syntax which leads to an error when trying to run a Braket Circuit with free parameters following the '[]'-naming. Therefore, I ensured that these will be removed before the ``ParamterExpression`` is passed to create a FreeParameterExpression out of it.
…t ``ParameterVector``

Assume this example: A one-qubit qiskit circuit with the parametrized Rx-gate as its only operation.
When using a qiskit ``ParameterVector``` of length 1 (i.e., params = ParameterVector("a", 1)), this would lead to the Parameter being called 'a[0]' which leads to a QASM 3 syntax error when trying to run the circuit.
The proposed changes ensure that the squared-parentheses '[]' are being removed in the parameter names.
@lukasvoss
Copy link
Contributor Author

Dear Lauren, dear Jean-Christophe,
I have implement some changes that are solving the errors arising from the qiskit naming convention of ParameterVector elements by removing the squared-parenthesis from the parameter name.

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.

On top of the comment above, we should also have tests in test_adapter.py. test_convert_parametric_qiskit_to_braket_circuit should be a good one to duplicate and adapt with ParameterExpression and ParameterVector.

qiskit_braket_provider/providers/adapter.py Outdated Show resolved Hide resolved
@jcjaskula-aws
Copy link
Collaborator

@lukasvoss I took the liberty to push commits to your PR. Let me know if you are satisfied with the current implementation.

@lukasvoss
Copy link
Contributor Author

@jcjaskula-aws Thanks for your contribution, looks good to me!

Copy link
Contributor Author

@lukasvoss lukasvoss left a comment

Choose a reason for hiding this comment

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

Will now be handled with renaming parameter values, adjusting for QASM3 compatibility by removing parenthesis and controlling for duplicates in the parameter naming.

@lukasvoss
Copy link
Contributor Author

@ALL: Thanks for all your contributions!

@jcjaskula-aws jcjaskula-aws merged commit 15e11d1 into qiskit-community:main Feb 12, 2024
7 checks passed
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.

5 participants