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

Add guide on operators and observables #3654

Merged
merged 18 commits into from
Feb 3, 2021
Merged

Conversation

rmlarose
Copy link
Contributor

@rmlarose rmlarose commented Jan 8, 2021

Fixes #3575. (Perhaps #1550 as well.)

As mentioned in the notebook, the intended audience for this guide is someone already familiar with these concepts, just not how they are defined/used in Cirq.

Let me know @balopat how close this is to what you had in mind! Comments and feedback of course welcome.

@rmlarose rmlarose requested review from cduck, vtomole and a team as code owners January 8, 2021 21:08
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Jan 8, 2021
@rmlarose rmlarose changed the title Add guide on operators and observables [WIP] Add guide on operators and observables Jan 8, 2021
@rmlarose rmlarose changed the title [WIP] Add guide on operators and observables Add guide on operators and observables Jan 11, 2021
@rmlarose
Copy link
Contributor Author

Now ready for review.

I learned that PauliSumCollector.collect which uses asyncio doesn't always mix well with notebooks - throws RuntimeError: This event loop is already running or a more mysterious papermill error (KeyError: 'language_info') - but collect_async seems to work fine.

},
"outputs": [],
"source": [
"qbit = cirq.LineQubit(0)\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: change to qubit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated here and throughout.

"outputs": [],
"source": [
"qbit = cirq.LineQubit(0)\n",
"unitary_operation = cirq.ops.X.on(qbit) # cirq.X can also be used for cirq.ops.X\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: change to qubit

"id": "6771bf1c439a"
},
"source": [
"Cirq makes a distinction between gates (independent of qubits) and operations (gates acting on qubits). Thus `cirq.X` is a gate where `cirq.X.on(qbit)` is an operation. See the [guide on gates](gates.ipynb) for more details and additional common unitaries defined in Cirq."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: change to qubit

},
"outputs": [],
"source": [
"measurement_operation = measurement.on(qbit)\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: change to qubit

"outputs": [],
"source": [
"circuit = cirq.Circuit(\n",
" cirq.H(qbit),\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: change to qubit

"source": [
"circuit = cirq.Circuit(\n",
" cirq.H(qbit),\n",
" cirq.depolarize(p=0.01).on(qbit),\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: change to qubit

"circuit = cirq.Circuit(\n",
" cirq.H(qbit),\n",
" cirq.depolarize(p=0.01).on(qbit),\n",
" cirq.measure(qbit)\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: change to qubit

},
"outputs": [],
"source": [
"qbit = cirq.LineQubit(0)\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: change to qubit

Copy link
Collaborator

@vtomole vtomole left a comment

Choose a reason for hiding this comment

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

This is fine to me.

Copy link
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

Thanks for this, it's a great start, good to see a comprehensive overview on this topic! I added some initial comments.

I wonder if we want to talk about projectors (cirq.KET_IMAG.projector()) here too?

" <td rowspan=\"3\">Operators</td>\n",
" <td>Unitary operators</td>\n",
" <td>`cirq.X`</td>\n",
" <td>[Gates](gates.ipynb) and [Custom gates](custom_gates.ipynb)</td>\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

These links don't seem to work well, because you are mixing HTML with markdown. You could try creating tables with the table syntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to markdown. (Only reason for html was rowspan which isn't supported by markdown, but this isn't needed with the new table as below.)

"id": "7cabbfa3ba55"
},
"source": [
"Quantum operations (or just *operators*) include unitary gates, measurements, and noisy channels. Every operator in Cirq is an instance of a `cirq.Operation` which supports the Kraus operator representation\n",
Copy link
Contributor

Choose a reason for hiding this comment

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

Mentioning cirq.Operation is confusing a bit, because gates don't inherit cirq.Operation. It would be more precise to say quantum operators that are acting on a given set of qubits implement cirq.Operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Used this wording here and throughout.

" <td></td>\n",
" </tr>\n",
"</tbody>\n",
"</table>"
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the Operators vs Observables split.
Item is too generic, mainly because the category of the items is in the first column. We could break it out to two separate tables - that would save a column as well. Actually observables might not even need a table. Also cirq.PauliSum is a generic way to express any operator, hermitian, non-hermitian, anything, not just observables. I feel like we should capture that somehow.

How about something like this:

Operators:

Operator Cirq representation Guides Examples
Unitary operators any class implementing the _unitary_ protocol Protocols, Gates and operations, custom gates cirq.Gate: cirq.X(qubit), cirq.CNOT(q0, q1), cirq.MatrixGate.on(qubit), cirq.Circuit (if it only contains unitary operations)
Measurement cirq.measure and cirq.MeasurementGate Gates and operations cirq.measure(cirq.LineQubit(0))
Quantum channels
  • Kraus operators (any class implementing the _channel_ protocol)
  • Pauli basis (cirq.PauliSum)
protocols cirq.DepolarizingChannel(p=0.2)(q0), cirq.X.with_probability(0.5)

Cirq also supports Observables on qubits that can be used to calculate expectation values on a given state.

  • You can use cirq.PauliString to express them
  • Or you can use cirq.PauliSum with real coefficients

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I like this and adopted it almost verbatim with just a few cosmetic changes.

@rmlarose
Copy link
Contributor Author

I wonder if we want to talk about projectors (cirq.KET_IMAG.projector()) here too?

Seems like a good idea - probably in the section on measurements? I'm thinking of a simple example showing measurements in a different basis, after describing cirq.measure_state_vector and cirq.measure_density_matrix which measure in the Z basis. Something like

prob_plus = np.trace(cirq.KET_PLUS.projector() @ rho).real

where rho is the same single-qubit density matrix from the previous example. Or maybe a two-qubit example is better. Is this what you had in mind?

@mpharrigan
Copy link
Collaborator

How did you fix KeyError: 'language_info' ?

@rmlarose
Copy link
Contributor Author

How did you fix KeyError: 'language_info' ?

Just to document for posterity: Looks like this was unrelated to asyncio as it popped up in #3669, see also #3603 (comment). So here I'm not sure why the error went away.

@rmlarose rmlarose requested a review from balopat January 26, 2021 21:48
@balopat
Copy link
Contributor

balopat commented Feb 3, 2021

I wonder if we want to talk about projectors (cirq.KET_IMAG.projector()) here too?

Seems like a good idea - probably in the section on measurements? I'm thinking of a simple example showing measurements in a different basis, after describing cirq.measure_state_vector and cirq.measure_density_matrix which measure in the Z basis. Something like

prob_plus = np.trace(cirq.KET_PLUS.projector() @ rho).real

where rho is the same single-qubit density matrix from the previous example. Or maybe a two-qubit example is better. Is this what you had in mind?

Hmm...I was just thinking about a separate section for Projector operators, which are also coming in form of KetBraSum operators soon(ish) #3386. Simulating measurement this way would be an interesting idea too!

I think this all is looking really good now. We can add the projectors as part of a separate issue, after we implemented the KetBraSum features. Then we can expand on pros/cons there - in the end KetBraSum should be a more generic and efficient, sparse representation for projectors.

@balopat balopat added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Feb 3, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Feb 3, 2021
@CirqBot CirqBot merged commit 03f3a2f into quantumlib:master Feb 3, 2021
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Feb 3, 2021
@rmlarose
Copy link
Contributor Author

rmlarose commented Feb 3, 2021

We can add the projectors as part of a separate issue, after we implemented the KetBraSum features. Then we can expand on pros/cons there - in the end KetBraSum should be a more generic and efficient, sparse representation for projectors.

Sounds good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/expectation-value cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Docs: Quantum operator representations
5 participants