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

Allow to normalize inputs on StatePreparation and Initialize #7189

Merged
merged 22 commits into from
Jun 20, 2023

Conversation

daniel-fry
Copy link
Contributor

Summary

Details and comments

@daniel-fry
Copy link
Contributor Author

The idea is that the user doesn't have to normalize their own input array and that by using normalize=True the user will understand that their input array needs to be normalized and that the Initializer will do it for them.

@Cryoris
Copy link
Contributor

Cryoris commented Nov 4, 2021

To fix the last errors you should run black, e.g. by calling make black in the terminal in the folder qiskit-terra. 🙂

@Cryoris
Copy link
Contributor

Cryoris commented Jan 27, 2022

@daniel-fry are you still pursuing this? 🙂

@daniel-fry
Copy link
Contributor Author

@daniel-fry are you still pursuing this? 🙂

Hi @Cryoris, yes I'm still pursuing this and I hope to finish it soon (as my first contribution to Qiskit). I just saw your last message about make black. Will work on this today. Might have a couple of questions. Thanks again.

@daniel-fry
Copy link
Contributor Author

@daniel-fry are you still pursuing this? 🙂

@Cryoris I can't find much for the "make black" command on Google. I assume it is coming from the black library for code style and I can follow this https://pypi.org/project/black/ ?

@mtreinish
Copy link
Member

@daniel-fry are you still pursuing this? slightly_smiling_face

@Cryoris I can't find much for the "make black" command on Google. I assume it is coming from the black library for code style and I can follow this https://pypi.org/project/black/ ?

I would suggest just using tox to do this as is suggested in the contributing guide: https://github.com/Qiskit/qiskit-terra/blob/main/CONTRIBUTING.md#pull-request-checklist. Assuming you've installed tox into your python environment (if you haven't pip install tox will handle that) you just run tox -eblack to run black which will automatically fix the code formatting. You can also use tox -elint-incr to check the other style/lint enforcement that CI runs.

@daniel-fry
Copy link
Contributor Author

@daniel-fry are you still pursuing this? slightly_smiling_face

@Cryoris I can't find much for the "make black" command on Google. I assume it is coming from the black library for code style and I can follow this https://pypi.org/project/black/ ?

I would suggest just using tox to do this as is suggested in the contributing guide: https://github.com/Qiskit/qiskit-terra/blob/main/CONTRIBUTING.md#pull-request-checklist. Assuming you've installed tox into your python environment (if you haven't pip install tox will handle that) you just run tox -eblack to run black which will automatically fix the code formatting. You can also use tox -elint-incr to check the other style/lint enforcement that CI runs.

OK thanks @mtreinish I have used tox and it reformatted the initiailizer.py file and gave me a "congratulations :)" but the check error is still there so I must have uploaded it wrong. Working on it...

@jakelishman
Copy link
Member

I think your git client has its line-endings setting configured incorrectly for collaboration (don't worry, it's not a big deal). I'm guessing you're developing on Windows - if so, you can run

git config core.autocrlf true

in the repository and that will change your setting for this repository only to the right thing. Unless you have a good reason not to, you probably want to make this setting apply to all your repositories, which you do with

git config --global core.autocrlf true

The effect of this command is that git will make sure that the files have "Windows-style" line-endings when you're actually editing them, but they get converted to Linux/Mac style when you commit. That's the normal setup in projects with lots of people collaborating.

After you've changed your settings (either way), you can tell git to fix the file. You do that with

git add --renormalize qiskit/extensions/quantum_initializer/initializer.py
git commit -m "Fix line endings"

and then push that commit to GitHub.

So you don't have to just trust me to run random commands on your computer, here's the documentation from GitHub and from git itself about those settings.

@daniel-fry
Copy link
Contributor Author

@jakelishman Thanks for the tip!

@daniel-fry
Copy link
Contributor Author

Hi @Cryoris @jakelishman , I've done the things you suggested. My PR is failing only 1 check now I think. How do I better understand why it's failing? I can see something about azure pipeline...

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

At the bottom of this page, you should see a list of tests across Azure (the blue rocket logo) and GitHub Actions. You can click on "details" on any one of them, which takes you to a GitHub summary page, and the most useful information is found by clicking the "View more details on Azure Pipelines" link that's on this new page. That gets you to Azure, where if you scroll down you should be able to see the failing job(s). If you keep clicking through the red crosses, you'll get to the logs - in this case it's a failure to build the documentation, with the error

/home/vsts/work/1/s/qiskit/extensions/quantum_initializer/initializer.py:
docstring of qiskit.extensions.quantum_initializer.initializer.initialize:25:
Definition list ends without a blank line; unexpected unindent.

Sphinx (the docs tool) errors can be pretty unintelligible at times. In this case it's because of the slightly odd indentation in the docstring of Initializer.__init__. You can run the documentation build yourself locally with tox -e docs, to try things out - it can be a bit frustrating at first to learn reStructuredText (RST) because it's a surprisingly finnicky language, but it's powerful in the end.

(Getting to the logs is a bit fiddly for some reason - both GitHub and Azure insert a couple of unnecessary landing pages, so you have to click about four times when you'd expect it to only be one.)


Please could you also add some tests of the new behaviour, including any failure paths (e.g. ensuring that we test that if you pass an unnormalized vector without normalize=True, it errors). The failure-path tests might already be in place, but we've not historically been perfect about them, so they might not be.

qiskit/extensions/quantum_initializer/initializer.py Outdated Show resolved Hide resolved
qiskit/extensions/quantum_initializer/initializer.py Outdated Show resolved Hide resolved
qiskit/extensions/quantum_initializer/initializer.py Outdated Show resolved Hide resolved
qiskit/extensions/quantum_initializer/initializer.py Outdated Show resolved Hide resolved
@daniel-fry
Copy link
Contributor Author

At the bottom of this page, you should see a list of tests across Azure (the blue rocket logo) and GitHub Actions. You can click on "details" on any one of them, which takes you to a GitHub summary page, and the most useful information is found by clicking the "View more details on Azure Pipelines" link that's on this new page. That gets you to Azure, where if you scroll down you should be able to see the failing job(s). If you keep clicking through the red crosses, you'll get to the logs - in this case it's a failure to build the documentation, with the error

/home/vsts/work/1/s/qiskit/extensions/quantum_initializer/initializer.py:
docstring of qiskit.extensions.quantum_initializer.initializer.initialize:25:
Definition list ends without a blank line; unexpected unindent.

Sphinx (the docs tool) errors can be pretty unintelligible at times. In this case it's because of the slightly odd indentation in the docstring of Initializer.__init__. You can run the documentation build yourself locally with tox -e docs, to try things out - it can be a bit frustrating at first to learn reStructuredText (RST) because it's a surprisingly finnicky language, but it's powerful in the end.

(Getting to the logs is a bit fiddly for some reason - both GitHub and Azure insert a couple of unnecessary landing pages, so you have to click about four times when you'd expect it to only be one.)

Please could you also add some tests of the new behaviour, including any failure paths (e.g. ensuring that we test that if you pass an unnormalized vector without normalize=True, it errors). The failure-path tests might already be in place, but we've not historically been perfect about them, so they might not be.

Thanks for the great advice and tips. I appreciate it a lot.
Could you send me a link for how to add a failure path test please? You mean something besides adding code to make sure it errors?

@jakelishman
Copy link
Member

Yeah, exactly. In this case, you expect that if you try to pass an unnormalised array (like np.array([1, 2, 3])) with normalize=False, then it'll raise QiskitError. The test is written something like

def test_initialize_errors_on_unnormalized_input(self):
    qc = QuantumCircuit(3)
    with self.assertRaisesRegex(QiskitError, "<part of the expected message>"):
        qc.initialize(np.array([1, 2, 3]), normalize=False)

The test will report a failure if that type of exception isn't raised, or if the message doesn't match. The documentation for unittest.TestCase.assertRaisesRegex is here.

@daniel-fry
Copy link
Contributor Author

@Cryoris @jakelishman Unfortunately I don't have the time to make progress on this and understand how to push it through. Could you take over this please?
If you could write a few sentences after to explain how you resolved the issues I would greatly greatly appreciate it! and may be able to finish next time. Thank you for your help.

@coveralls
Copy link

coveralls commented Mar 21, 2022

Pull Request Test Coverage Report for Build 5323977910

  • 13 of 13 (100.0%) changed or added relevant lines in 2 files are covered.
  • 7 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.006%) to 85.97%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 1 91.14%
crates/qasm2/src/parse.rs 6 97.58%
Totals Coverage Status
Change from base Build 5320987396: -0.006%
Covered Lines: 71480
Relevant Lines: 83145

💛 - Coveralls

@Cryoris
Copy link
Contributor

Cryoris commented Mar 22, 2022

It would probably be good to hold this off until #7666 is merged, since this here is the smaller change and will likely be easier to integrate 🙂

@Cryoris Cryoris added the on hold Can not fix yet label Mar 22, 2022
@HuangJunye HuangJunye added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jun 21, 2022
@Cryoris Cryoris removed the on hold Can not fix yet label Aug 8, 2022
@Cryoris
Copy link
Contributor

Cryoris commented Aug 8, 2022

This is not blocked anymore, so we can proceed if you're still interested @daniel-fry! Since Initialize was split partially into StatePreparation, the functionality would probably have to be moved there 🙂

@daniel-fry
Copy link
Contributor Author

daniel-fry commented Aug 22, 2022

This is not blocked anymore, so we can proceed if you're still interested @daniel-fry! Since Initialize was split partially into StatePreparation, the functionality would probably have to be moved there 🙂

Hi @Cryoris ,
I think the code itself is complete. Thanks for all your help.

Unfortunately I don't have time to push it through the quality checks. Will you be able to do this? If there are any blocks I'd be grateful if you could tell me what they are and how to overcome them for next time.

@jakelishman jakelishman linked an issue Mar 20, 2023 that may be closed by this pull request
@Cryoris Cryoris changed the title Added func to normalize input vector in Initializer Allow to normalize inputs on StatePreparation and Initialize May 9, 2023
@Cryoris Cryoris requested a review from jakelishman May 9, 2023 12:21
@Cryoris Cryoris added the Changelog: New Feature Include in the "Added" section of the changelog label May 9, 2023
@jakelishman jakelishman added this to the 0.25.0 milestone Jun 20, 2023
@jakelishman jakelishman added this pull request to the merge queue Jun 20, 2023
Merged via the queue into Qiskit:main with commit f995814 Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog Community PR PRs from contributors that are not 'members' of the Qiskit repo
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Initialize normalized state / state normalization feature
6 participants