documentation icon indicating copy to clipboard operation
documentation copied to clipboard

standardize how we write import statements

Open acastellane opened this issue 1 year ago • 10 comments

URL to the relevant documentation

https://docs.quantum.ibm.com/guides/hello-world#step-2-optimize-the-circuits-and-operators

Select all that apply

  • [ ] typo
  • [ ] code bug
  • [ ] out-of-date content
  • [ ] broken link
  • [x] other

Describe the fix.

Add the importation of generate_preset_pass_manager to facilitate copy paste of this section. Otherwise as it is written very early in the initial importations, we might miss it if we ignore the initial setup and then get a missing lib error.

from qiskit_ibm_runtime import QiskitRuntimeService
**from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager**

It then mimics the previous case with from qiskit.quantum_info import SparsePauliOp

Thanks

acastellane avatar Oct 22 '24 17:10 acastellane

@acastellane, the idea is that you'd be doing the whole tutorial, and since we do the import in step 1, we would typically not import it again. Similarly, we don't re-import. However, since we've spread out the other imports throughout the notebook, I think it would be reasonable to move the import to this code, where it's used. @Eric-Arellano does that seem reasonable?

beckykd avatar Oct 23 '24 18:10 beckykd

@Eric-Arellano does that seem reasonable?

I think the most important thing is to be consistent. I personally am more of a fan of all imports at the top-of-the-file:

  • it matches how you would write a program, where all imports are upfront
  • it is less confusing when you reuse an import multiple times in a file. For example, we use QuantumCircuit in multiple code blocks. If you didn't realize it was imported in an earlier code block, you might be confused. Compare that to having all the imports up-front.

However, others like I think @javabster prefer having imports closer to the code's usage. We should finalize which of the two styles we prefer and then consolidate around it.

Eric-Arellano avatar Oct 23 '24 18:10 Eric-Arellano

I personally am more of a fan of all imports at the top-of-the-file

Yep; I know you've told me that before. I wonder whether this is a special circumstance because it's Hello World, or should I move them all. I know that's your preference, so I have been putting them all at the top. I'll tag this as needs discussion.

beckykd avatar Oct 23 '24 18:10 beckykd

Hi, consistency is key. Lets choose a way and keep it. Either one seems good with advantages.

Thanks for the tremendous work you all are doing !

acastellane avatar Oct 24 '24 07:10 acastellane

Yeah this is something that every dev has their own opinion on and there is no obvious best way. It's not possible to please everyone so I agree we should just aim for consistency. I believe the majority of the time we do the "all imports at the top" approach so I think we should stick to that (it also keeps it consistent with how they do things in tutorials).

javabster avatar Oct 24 '24 14:10 javabster

so to reframe this issue, whats needed is: take out all the import statements from the individual code snippets and move it to an "imports" section at the top

javabster avatar Oct 24 '24 14:10 javabster

I want to make a case for (re-)including the import as @acastellane suggests (but in a code comment), because Hello World is an exception to how we normally do things in the guides (and noting that since this issue was opened, Hello World has been moved to tutorials). I see value in helping a new user connect specific imports with the relevant code. Not a hill I'd die on, though, if we want to raise consistency as the higher priority! - I also invite input from @miamico / @nathanearnestnoble on whether pedagogically it matters much.

I opened a PR - originally to move all imports to the top, but then had second thoughts and instead drafted the code comment - I can change it back to moving all import statements if that is what we conclude.

abbycross avatar Oct 06 '25 15:10 abbycross

I'm generally more inclined for the "all imports up top" option as that makes it easy for users to copy paste code from the notebook knowing they can find all imports in one place. Agree that "dispersed" imports are more legible. any other thoughts?

miamico avatar Oct 07 '25 15:10 miamico

I see value in helping a new user connect specific imports with the relevant code.

The thing is, once you import something once, you never have to re-import it to use the symbol again. Imagine QuantumCircuit: they would see the import the first time it's used, but not realize what the import in later code blocks. This is why I personally like all imports being at the top: it gives you a single spot to see where every symbol comes from.

Eric-Arellano avatar Oct 07 '25 16:10 Eric-Arellano

Thanks for everyone's thoughts - Seems like it would be overly pedantic to spread out the imports. I'll move them to the top!

abbycross avatar Oct 07 '25 17:10 abbycross