oreilly-qc / oreilly-qc.github.io

Code samples for Programming Quantum Computers, from O'Reilly Media
135 stars 62 forks source link

Discard() #51

Open NedaABD opened 1 year ago

NedaABD commented 1 year ago

Hello, I hope you are doing well. I wonder to know the exact function of discard() in example 2-4.

In the cheatsheet it is written: qc.discard() Removes a specified qubit from the QPU. Can be used as a method on the qc object or a QInt object. If called without an argument then discards all qubits associated with the parent object.

my question is why do we remove all qubits we just allocated?

emipc commented 3 months ago

I had the same question. In fact, if you remove this line, the example still works.

machinelevel commented 3 months ago

Hi Neda and Emi, thanks for your notes! You're correct... it looks like that was left in by me by mistake. As you say, it's harmless, but here's more info:

The qc.discard() and qc.nop() in QCEngine exist mainly for diagramming purposes. Discard turns the lines soft, and nop provides some space.

...so if we add this code after the reset:

qc.nop();
qc.write(0);
qc.nop();
qc.discard();
qc.nop();

...we can get an effect like this, where the lines are "discarded" (visibly deactivated but otherwise unaffected):

image

machinelevel commented 3 months ago

I've fixed it in the repo, so it should eventually reflect in the sample page. Thank you!