CyberIntel ⬡ News
★ Saved ◆ Cyber Reads
← Back ◌ Quantum Computing Mar 22, 2020

How to calculate the probabilities of observing quantum states using the "expectation_from_wavefunction" in cirq (and why)

Quantum Computing SE Archived Mar 16, 2026 ✓ Full text saved

I am currently simulating some quantum circuits, and want to calculate the probabilities of observing each individual state. I am able to use Cirq for this, and calculate it using $P_{00} = |\alpha|^2$ . Code: import cirq import sympy x0, x1 = sympy.symbols('x0 x1') q = cirq.GridQubit.rect(1, 2) circuit = cirq.Circuit( cirq.rx(x0).on(q[0]), cirq.rx(x1).on(q[1]), cirq.ry(3.14/4).on(q[0]), cirq.ry(3.14/4).on(q[1])) resolver = cirq.ParamResolver({x0: 0.2, x1: 0.3}) simulator = cirq.Simulator() resu

Full text archived locally
✦ AI Summary · Claude Sonnet


    How to calculate the probabilities of observing quantum states using the "expectation_from_wavefunction" in cirq (and why) Ask Question Asked 5 years, 11 months ago Modified yesterday Viewed 670 times 1 I am currently simulating some quantum circuits, and want to calculate the probabilities of observing each individual state. I am able to use Cirq for this, and calculate it using P 00 =|α | 2 𝑃 00 = | 𝛼 | 2 . Code: import cirq import sympy x0, x1 = sympy.symbols('x0 x1') q = cirq.GridQubit.rect(1, 2) circuit = cirq.Circuit( cirq.rx(x0).on(q[0]), cirq.rx(x1).on(q[1]), cirq.ry(3.14/4).on(q[0]), cirq.ry(3.14/4).on(q[1])) resolver = cirq.ParamResolver({x0: 0.2, x1: 0.3}) simulator = cirq.Simulator() results = simulator.simulate(program=circuit, param_resolver=resolver, qubit_order=q).final_state print("Internal quantum state:", results) print("Probabilities of observing each state:", [abs(x)**2 for x in results]) Output: internal quantum state: [0.8377083+0.08743566j 0.3529709-0.11249491j 0.35297093-0.06251574j 0.13120411-0.08743566j] probabilities of observing each state: [0.7094002059173512, 0.13724355108492148, 0.12849669756020887, 0.024859516013751914] However, in multiple tutorials (for instance from TFQ) I see the use of "expectation_from_wavefunction": z0 = cirq.Z(q[0]) qubit_map={q[0]: 1, q[1]: 1} z0.expectation_from_wavefunction(results, qubit_map).real output: 0.6757938265800476 My question: How can I use expectation_from_wavefunction to obtain the probabilities of observing the individual states ( P 00 , P 01 , P 10 , P 11 𝑃 00 , 𝑃 01 , 𝑃 10 , 𝑃 11 )? Bonus question: why would I favor this approach? simulationcirq Share Improve this question Follow asked Mar 22, 2020 at 9:24 Thomas Hubregtsen 6241 1 gold badge 4 4 silver badges 11 11 bronze badges Why would want to use "expectation_from_wavefunction()" to calculate the probabilities when you can just do what you did; square the values of your wavefunction? The former takes more work than the latter. –  Victory Omole Commented Mar 22, 2020 at 14:24 @VictoryOmole Because in the tutorial of TFQ that I refer to, they only use the "expectation_from_wavefunction". All following examples build upon it. I think it helps with batching, but I am not sure. –  Thomas Hubregtsen Commented Mar 22, 2020 at 21:06 sorry, forgot to mention: the top approach works in Cirq. I am trying to run Tensorflow Quantum, and all tutorials here rely on this method. –  Thomas Hubregtsen Commented Mar 22, 2020 at 21:23 Tensorflow Quantum combines Tensorflow with Cirq. If you can "import tfq" you can "import cirq" and thus use all the functionality in Cirq. –  Victory Omole Commented Mar 23, 2020 at 16:14 expectation_from_wavefunction is used when you don't want to write the logic for yourself. This is more useful in cases with multi-qubit observables involving the X and Y axies. –  Craig Gidney Commented Apr 21, 2020 at 22:56 Add a comment 2 Answers Sorted by: Highest score (default) Date modified (newest first) Date created (oldest first) 0 why would I favor this approach? The Expectation value is defined as ⟨A⟩=⟨ψ|A|ψ⟩ ⟨ 𝐴 ⟩ = ⟨ 𝜓 | 𝐴 | 𝜓 ⟩ where ψ 𝜓 is the wavefunction and A 𝐴 is the operator. Use "expectation_from_wavefunction()" if you don't want to write code that calculates ⟨ψ|A|ψ⟩ ⟨ 𝜓 | 𝐴 | 𝜓 ⟩ . Share Improve this answer Follow answered Mar 22, 2020 at 14:28 Victory Omole 2,7061 1 gold badge 11 11 silver badges 28 28 bronze badges Thanks. I see now that the bottom approach calculates the expectation value, which is defined as ⟨𝐴⟩=⟨𝜓|𝐴|𝜓⟩. But how does this relate to the answer from the top approach? Because I see no relation between the numeric outcomes. For a 1-qubit system, would the expectation value not give me the probability of observing the qubit in the state 1? –  Thomas Hubregtsen Commented Mar 22, 2020 at 21:49 If you don't see the relation, what calculation did you perform? What do you get? –  Victory Omole Commented Mar 23, 2020 at 17:00 The code and its output should be shown in my question. I embed using Rx followed by an Ry. The top approach provides: internal quantum state: [0.8377083+0.08743566j 0.3529709-0.11249491j 0.35297093-0.06251574j 0.13120411-0.08743566j] probabilities of observing each state: [0.7094002059173512, 0.13724355108492148, 0.12849669756020887, 0.024859516013751914] The bottom approach (that seems to have an extra Rz, which I have also experimented with in the top approach to no affect) gives as output for the real component 0.6757938265800476. –  Thomas Hubregtsen Commented Mar 23, 2020 at 21:20 I meant: did you take the wavefunction and do a pen-and-paper calculation of that expectation value and get the same answer that z0.expectation_from_wavefunction(results, qubit_map).real gives you? –  Victory Omole Commented Mar 23, 2020 at 23:17 Add a comment 0 The expectation value that the system provided me with was 0.6757938265800476. This was in a range of [-1,1]. Mapping this to a range of [0,1]: (0.6757938265800476+1)/2=0.837896913290024. The expectation value was the expectation value for qubit 0. This implies that it observed qubit 0 in state 0 83.7896913290024% of the time, and 1-0.837896913290024 in state 1. The system also printed the following probabilities: P 00 =0.7094002059173512 𝑃 00 = 0.7094002059173512 P 01 =0.13724355108492148 𝑃 01 = 0.13724355108492148 P 10 =0.12849669756020887 𝑃 10 = 0.12849669756020887 P 11 =0.024859516013751914 𝑃 11 = 0.024859516013751914 The probability to observe qubit 0 (indexing right to left) in state 0: E q0 =0.8378969=0.7094002+0.1284966= P 00 + P 10 𝐸 𝑞 0 = 0.8378969 = 0.7094002 + 0.1284966 = 𝑃 00 + 𝑃 10 To answer the question "How can I use expectation_from_wavefunction to obtain the probabilities of observing the individual states": you can't. You can only relate this particular expectation value (with Pauli Z) to a set of state probabilities, but not to individual ones without extra information (such as observables in different computational basis) "Why would I favor this approach": access to the internal quantum state is not realistic with quantum system, and would require more computation. P.S. this is my current interpretation which I expect to be right on a high level. I think I still have details wrong, such as parts of the explanation. Very open to opinions. Share Improve this answer Follow edited Apr 22, 2020 at 11:08 answered Apr 22, 2020 at 11:02 Thomas Hubregtsen 6241 1 gold badge 4 4 silver badges 11 11 bronze badges Add a comment Your Answer Sign up or log in Sign up using Google Sign up using Email and Password Post as a guest Name Email Required, but never shown Post Your Answer By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy. Start asking to get answers Find the answer to your question by asking. Ask question Explore related questions simulationcirq See similar questions with these tags. The Overflow Blog Open source for awkward robots Domain expertise still wanted: the latest trends in AI-assisted knowledge for... Featured on Meta Logo updates to Stack Overflow's visual identity Related 6 Using parametrised values and 'Symbols' in Cirq 1 Normalization of the quantum state in Cirq 0 VQE for Beginners : Using Tutorial and Cirq 4 Access and set_state during quantum computation simulation in cirq 3 Multiplying quantum circuits in cirq using * and computing "expectation values" 1 Use simulator.simulate() to obtain probability distribution instead of simulator.run() (cirq) Hot Network Questions In "Mecha Marathon" does it matter how many times the player winds up the racer? Why is there a dash before "tu" in "Quel quartier préfères‑tu ?" Cockroach carrying a spy video camera in a movie? Accounts for changing Kinetic energy of electrons in a circuit How could an egg survive below freezing temperatures for long periods of time? Why are dialetheias sometimes described as “nondualisms”? Mirror stopped working apt-key is removed in Debian 13. How to list keys? "Out of malice"? Cardinality of the set of isomorphism classes of 2-generated groups Commentaries on the Organon? Suing the federal postal service for regularly failing to deliver mail to a customer Pascal's Triangle ! BCsort: A fast, statistical, in-place ternary distribution sort How can I raise the handlebars on this bike? LilyPond: Unusually nested repeats How random is RandomInteger[{0,B}]? What do I do? I am underqualified for my job Pi Day: estimating pi using probability Typical length and structure of “selection criteria responses” in Australian academic job applications (math/statistics) Is compound assignment on atomic variables guaranteed to be atomic prior to C23? My opening sentence is awkward, how can I fix it? How do I solve the Alignment Control Centre puzzle? Add two rational numbers... esoterically Question feed By continuing to use this website, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. By exiting this window, default cookies will be accepted. To reject cookies, select an option from below. Customize settings Cookie Consent Preference Center When you visit any of our websites, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences, or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and manage your preferences. Please note, blocking some types of cookies may impact your experience of the site and the services we are able to offer. Cookie Policy Accept all cookies Manage Consent Preferences Strictly Necessary Cookies Always Active These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information. Targeting Cookies Targeting Cookies These cookies are used to make advertising messages more relevant to you and may be set through our site by us or by our advertising partners. They may be used to build a profile of your interests and show you relevant advertising on our site or on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. Performance Cookies Performance Cookies These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance. Functional Cookies Functional Cookies These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly. Cookie List Clear checkbox label label Apply Cancel Consent Leg.Interest checkbox label label checkbox label label checkbox label label Necessary cookies only Confirm My Choices
    💬 Team Notes
    Article Info
    Source
    Quantum Computing SE
    Category
    ◌ Quantum Computing
    Published
    Mar 22, 2020
    Archived
    Mar 16, 2026
    Full Text
    ✓ Saved locally
    Open Original ↗