Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Reverting the Allocate() change. (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzminrobin authored Oct 19, 2021
1 parent 76dddfd commit e8c2ed0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
31 changes: 26 additions & 5 deletions src/Simulation/Common/QubitManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,13 @@ public void Disable(IQArray<Qubit> qubitsToDisable)
/// Allocates a qubit.
/// Returns null if the qubit cannot be allocated.
/// </summary>
protected virtual Qubit Allocate(bool usedOnlyForBorrowing)
protected virtual Qubit? Allocate(bool usedOnlyForBorrowing)
{
if (free == None)
{
if (!MayExtendCapacity)
{
throw new NotEnoughQubits(1, this.FreeQubitsCount);
return null;
}

long oldNumQubits = NumQubits;
Expand Down Expand Up @@ -398,7 +398,12 @@ protected virtual Qubit Allocate(bool usedOnlyForBorrowing)
/// </summary>
public Qubit Allocate()
{
return Allocate(usedOnlyForBorrowing: false);
Qubit? qb = Allocate(usedOnlyForBorrowing: false);
if (qb == null)
{
throw new NotEnoughQubits(1, this.FreeQubitsCount);
}
return qb;
}

/// <summary>
Expand All @@ -424,7 +429,15 @@ public IQArray<Qubit> Allocate(long numToAllocate)
}
for (int i = 0; i < numToAllocate; i++)
{
Qubit allocated = Allocate(usedOnlyForBorrowing: false);
Qubit? allocated = Allocate(usedOnlyForBorrowing: false);
if (allocated == null)
{
for (int k = 0; k < i; k++)
{
Release(result[k], wasUsedOnlyForBorrowing: false);
}
throw new NotEnoughQubits(numToAllocate, this.FreeQubitsCount);
}
result.Modify(i, allocated);
}

Expand Down Expand Up @@ -581,7 +594,15 @@ internal IQArray<Qubit> Borrow(long numToBorrow, HashSet<Qubit> qubitsInUse)
{ // Not enough qubits to borrow. Allocate what was not borrowed.
for (long i = numBorrowed; i < numToBorrow; i++)
{
Qubit allocated = Allocate(usedOnlyForBorrowing: true);
Qubit? allocated = Allocate(usedOnlyForBorrowing: true);
if (allocated == null)
{
for (long k = numBorrowed; k < i; k++)
{
Release(borrowed[(int)k], wasUsedOnlyForBorrowing: true);
}
throw new NotEnoughQubits(numToBorrow, numBorrowed + this.FreeQubitsCount);
}
borrowed.Modify(i, allocated);
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/Simulation/Simulators/CommonNativeSimulator/QubitManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ public override Qubit CreateQubitObject(long id)
return new QSimQubit((int)id, Simulator);
}

protected override Qubit Allocate(bool usedOnlyForBorrowing)
protected override Qubit? Allocate(bool usedOnlyForBorrowing)
{
Qubit qubit = base.Allocate(usedOnlyForBorrowing);
Debug.Assert(Simulator != null);
Simulator.AllocateOne((uint)qubit.Id);
Qubit? qubit = base.Allocate(usedOnlyForBorrowing);
if (qubit != null)
{
Debug.Assert(Simulator != null);
Simulator.AllocateOne((uint)qubit.Id);
}
return qubit;
}

Expand Down

0 comments on commit e8c2ed0

Please sign in to comment.