-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add user best practices documentation #5604
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
Merged
CirqBot
merged 8 commits into
quantumlib:master
from
dstrain115:docs_user_best_practices
Jun 24, 2022
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad52de7
Add user best practices documentation
dstrain115 194b6a8
Review comments.
dstrain115 5704a98
Merge branch 'master' into docs_user_best_practices
dstrain115 4c9c8ea
Fix typo and remove trailing blank line
pavoljuhas 820e46e
Update docs/start/best_practices.md
dstrain115 258d359
Update docs/start/best_practices.md
dstrain115 ae2e7f9
Update docs/start/best_practices.md
dstrain115 e6ef3e7
Merge branch 'master' into docs_user_best_practices
CirqBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ and have a discussion with the maintainers. Mention that you are willing to writ | |
* Make sure to share your doc with [email protected] for comments. | ||
* Link the RFC in your issue. | ||
4. Recruiting a sponsor: | ||
* A sponsor must be a maintainer of the project or the product manager (currently Alan Ho). | ||
* A sponsor must be a maintainer of the project or the product manager. | ||
* Write a comment in your Github issue that calls out that you are "Looking for a sponsor". A maintainer will mark the issue with a label: "rfc/needs-sponsor". | ||
* While it might take some time to get a maintainer to sponsor your RFC, it is essential, as the sponsor will facilitate the process for reviewing your design. | ||
* Tips to recruit a sponsor: 1) keep commenting on the issue weekly 2) attend Cirq Cynq and push for a sponsor. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Best Practices | ||
|
||
This page describes some of the best practices when using the Cirq library. | ||
Following these guidelines will help you write code that is more performant and | ||
less likely to break from version to version. Many of these rules apply to | ||
other python libraries as well. | ||
|
||
## Use top-level constructs | ||
|
||
The Cirq library is designed so that important user-facing classes and objects | ||
are exposed at the package level. Avoid referencing module names within Cirq. | ||
|
||
For instance, use `cirq.X` and **not** `cirq.ops.X`. The second version will | ||
break if we rename modules or move classes around from version to version. | ||
|
||
## Do not use private member variables of classes | ||
|
||
Any member of a class that is prefixed with an underscore is, by convention, a | ||
private variable and should not be used outside of this class. For instance, | ||
`cirq.XPowGate._dimension` should not be accessed or modified, since it is a | ||
private member of that class. Using or modifying these values could result in | ||
unexpected behavior or in broken code. | ||
|
||
## Do not mutate "immutable" classes | ||
|
||
While python's flexibility allows developers to modify just about anything, it | ||
is bad practice to modify classes that are designed to be immutable. Doing so | ||
can violate assumptions made in other parts of the library. | ||
|
||
In particular, attributes of `cirq.Gate`, `cirq.Operation`, `cirq.Moment`, and | ||
`cirq.ParamResolver` should not be modified after creation. If these objects | ||
need to be modified, a new object should be created instead. | ||
|
||
Violating this principle could cause problems in other parts of the code. For | ||
instance, changing the qubits of an `cirq.Operation` could cause a `cirq.Moment` | ||
that contains this Operation to have two Operations with the same qubits (which | ||
is not allowed). | ||
|
||
Note that `Circuit` objects can be modified, but `FrozenCircuit` objects cannot. | ||
|
||
## Be mindful of exponential scaling | ||
|
||
Many algorithms and procedures in quantum computing scale in an exponential | ||
pattern. Cirq is designed for the noisy intermediate-scale quantum computing | ||
(NISQ) regime. Creating circuits with hundreds or thousands of qubits may | ||
surpass the capabilities of this library. | ||
|
||
Even with smaller numbers of qubits, simulation and other tasks can very quickly | ||
consume resources and time. The difference between a one second and an hour's | ||
computation can be as few as ten qubits. | ||
|
||
## What you see is what you get | ||
|
||
Cirq tries to be as true to the specified circuits as possible, especially with | ||
respect to hardware execution. Cirq highly discourages any hidden automatic | ||
decomposition, compilation, or other modification of a circuit that is unknown | ||
to the user. Any modification or transformation to the circuit should be | ||
initiated by the user of the library. | ||
|
||
This philosophy is important for many use cases. For instance, certain | ||
benchmarking algorithms rely on the fact that gate sequences will not be optimized, | ||
even if the circuit is nominally inefficient. | ||
|
||
Of course, Cirq provides routines and functions for compilation and | ||
transformation of circuits. Users can and should call these routines. However, | ||
Cirq and resulting hardware integrations should not modify the circuits without | ||
the user's "permission". | ||
|
||
## Other style and performance guidelines | ||
|
||
* Use `cirq.CircuitOperation` to more compactly define large, repeated | ||
circuits. This can save space and time for analysis of larger circuits. | ||
* For hardware execution of multiple circuits, prefer using `run_sweep` to | ||
run variants of circuits. When not possible, try using `run_batch`. Using | ||
these methods gives the hardware service the most opportunity to optimize | ||
the execution of circuits and can result in much faster execution. | ||
Read more details on the [Parameter Sweeps](/cirq/simulate/params) page. | ||
* Consider defining and allocating qubits at the beginning of your code or | ||
function, then applying gates and circuits to those qubits. While not | ||
required, this style can produce cleaner code. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.