-
Notifications
You must be signed in to change notification settings - Fork 90
Remove BitStates.hpp #581
Remove BitStates.hpp #581
Changes from all commits
3bfaaa0
a1de729
f68b6ff
34fd856
8aaddf9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,8 +8,6 @@ | |
| #include "QSharpSimApi_I.hpp" | ||
| #include "SimFactory.hpp" | ||
|
|
||
| #include "BitStates.hpp" | ||
|
|
||
| namespace Microsoft | ||
| { | ||
| namespace Quantum | ||
|
|
@@ -24,7 +22,7 @@ namespace Quantum | |
|
|
||
| // State of a qubit is represented by a bit in states indexed by qubit's id, | ||
| // bits 0 and 1 correspond to |0> and |1> states respectively. | ||
| BitStates states; | ||
| std::vector<bool> states; | ||
|
|
||
| // The clients should never attempt to derefenece the Result, so we'll use fake | ||
| // pointers to avoid allocation and deallocation. | ||
|
|
@@ -67,21 +65,23 @@ namespace Quantum | |
| Qubit AllocateQubit() override | ||
| { | ||
| this->lastUsedId++; | ||
| this->states.ExtendToInclude(this->lastUsedId); | ||
| this->states.emplace_back(false); | ||
| return reinterpret_cast<Qubit>(this->lastUsedId); | ||
| } | ||
|
|
||
| void ReleaseQubit(Qubit qubit) override | ||
| { | ||
| const long id = GetQubitId(qubit); | ||
| assert(id <= this->lastUsedId); | ||
| assert(!this->states.IsBitSetAt(id)); | ||
| assert(!this->states.at(id)); | ||
| this->lastUsedId--; | ||
| this->states.pop_back(); | ||
| } | ||
|
Comment on lines
72
to
79
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have an impression that this function works correctly for the last-most qubit id only. But if we release a qubit "in the middle" then still the last-most qubit will be released. E.g. we call
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's correct, but in this case I'm relying on Q#'s strict qubit scoping for a shortcut. In Q#, every qubit has a scope, either implied or explicit, such that any qubit allocations are always released in the reverse order they were allocated. This does mean that the ToffoliSimulator could exhibit the bug you describe if it is used for QIR that was generated from something other than Q# (or written manually). However, because the simulators are packages as part of That said, this is a very subtle requirement that might not be obvious, and I'm open to further discussion. @bettinaheim, any concerns or suggestions about how to handle this?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the qubits are always deallocated in the reverse order of allocation then we are fine here. Can mark this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case the parameter ( void ReleaseQubit(Qubit qubit) override |
||
|
|
||
| std::string QubitToString(Qubit qubit) override | ||
| { | ||
| const long id = GetQubitId(qubit); | ||
| return std::to_string(id) + ":" + (this->states.IsBitSetAt(id) ? "1" : "0"); | ||
| return std::to_string(id) + ":" + (this->states.at(id) ? "1" : "0"); | ||
| } | ||
|
|
||
| /// | ||
|
|
@@ -120,15 +120,15 @@ namespace Quantum | |
| /// | ||
| void X(Qubit qubit) override | ||
| { | ||
| this->states.FlipBitAt(GetQubitId(qubit)); | ||
| this->states.at(GetQubitId(qubit)).flip(); | ||
| } | ||
|
|
||
| void ControlledX(long numControls, Qubit* const controls, Qubit qubit) override | ||
| { | ||
| bool allControlsSet = true; | ||
| for (long i = 0; i < numControls; i++) | ||
| { | ||
| if (!this->states.IsBitSetAt(GetQubitId(controls[i]))) | ||
| if (!this->states.at(GetQubitId(controls[i]))) | ||
| { | ||
| allControlsSet = false; | ||
| break; | ||
|
|
@@ -137,7 +137,7 @@ namespace Quantum | |
|
|
||
| if (allControlsSet) | ||
| { | ||
| this->states.FlipBitAt(GetQubitId(qubit)); | ||
| this->states.at(GetQubitId(qubit)).flip(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -153,7 +153,7 @@ namespace Quantum | |
| } | ||
| if (bases[i] == PauliId_Z) | ||
| { | ||
| odd ^= (this->states.IsBitSetAt(GetQubitId(targets[i]))); | ||
| odd ^= (this->states.at(GetQubitId(targets[i]))); | ||
| } | ||
| } | ||
| return odd ? one : zero; | ||
|
|
||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this does have a change in behavior here, where previously an allocated qubit that was released would never be used again, here we allow for qubit "reuse" by decrementing the
lastUsedIdmember and shrinking the vector such that future allocations can use that id again. This will prevent the bit vector from always growing, such that Q# like this:Will now have a bit vector of at most length 1 instead of length 10.