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

Support np.sign for parameter #10360

Closed
nkanazawa1989 opened this issue Jun 29, 2023 · 9 comments · Fixed by #10571
Closed

Support np.sign for parameter #10360

nkanazawa1989 opened this issue Jun 29, 2023 · 9 comments · Fixed by #10571
Assignees
Labels
type: feature request New feature or request

Comments

@nkanazawa1989
Copy link
Contributor

What should we add?

I sometime want to check sign of parameter value, but this is not currently supported.

from qiskit.circuit import Parameter
import numpy as np

v = Parameter("value")
np.sign(v)

(temporary hack)

from qiskit.circuit import ParameterExpression
import symengine as sym

sign_of_v = ParameterExpression(
    symbol_map={v: v._symbol_expr},
    expr=sym.sign(v._symbol_expr),
)

It would be great if Qiskit can support this without above ugly hack.

@SamD-1998
Copy link
Contributor

Hey, I can work on this. Can you assign it to me?

@SamD-1998
Copy link
Contributor

I can add a method inside the function definition of Parameter. Perhaps that's simpler and prevents you from importing numpy. Would you want that?

@wshanks
Copy link
Contributor

wshanks commented Jul 21, 2023

I wouldn't use numpy. I would do something similar to what is done for other functions like this one:

https://github.com/Qiskit/qiskit-terra/blob/2b225d3713a8ef160a38e8fbfbc7d08a0b8a4682/qiskit/circuit/parameterexpression.py#L377-L386

The main thing is to add a sign method so that the user does not need to access the private _symbol_expr attribute.

@SamD-1998
Copy link
Contributor

Hi, I think I have a working code that implements this. The contribution guidelines say that I have to open a Draft pull request and prepend the title with the WIP tag. How do I do this?

@wshanks
Copy link
Contributor

wshanks commented Jul 28, 2023

Opening a draft pull request instead of a normal pull request just requires selecting an option from a drop down on open pull request page. See the screenshot on this page.

Prepending the title just means putting the text "WIP" at the beginning of the title text box when opening the pull request.

Opening a pull request is a little more involved and there are several ways to do it. Maybe this page from GitHub is a good starting point.

@SamD-1998
Copy link
Contributor

Hi, I tried pushing my branch to a new remote branch in this qiskit-terra repo but I got a permission denied error. What should I do?

@wshanks
Copy link
Contributor

wshanks commented Aug 4, 2023

Yes, only a limited set of people can modify the Qiskit/qiskit-terra repo. You need to create a fork of the repo in your account, add your branch to your repo, and then create the pull request in the Qiskit/qiskit-terra repo from your fork (probably called SamD-1998/qiskit-terra). There is some description of forks here and about creating a pull request across forks here.

@SamD-1998
Copy link
Contributor

I've initiated a PR in the base repository: Qiskit/qiskit-terra and base: main. I might have to make a note somewhere about how to use this feature. Do I need to include a docstring somewhere to mention it to a user?

@wshanks
Copy link
Contributor

wshanks commented Aug 4, 2023

Yes, adding a docstring is good. We can move the discussion to #10571.

@jakelishman jakelishman linked a pull request Aug 4, 2023 that will close this issue
github-merge-queue bot pushed a commit that referenced this issue Sep 21, 2023
* Add sign() method to ParameterExpression

This method allows applying the sign operation to an expression with
possibly unassigned parameters.

* Update releasenotes/notes/added-sign-feature-parameterexpression-57693dd69103dc8c.yaml

Co-authored-by: Will Shanks <wshaos@posteo.net>

* Update releasenotes/notes/added-sign-feature-parameterexpression-57693dd69103dc8c.yaml

Co-authored-by: Will Shanks <wshaos@posteo.net>

* Update releasenotes/notes/added-sign-feature-parameterexpression-57693dd69103dc8c.yaml

Co-authored-by: Will Shanks <wshaos@posteo.net>

---------

Co-authored-by: Will Shanks <wshaos@posteo.net>
Co-authored-by: Will Shanks <willshanks@us.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants