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

Commit 76dddfd

Browse files
authored
Broken up QuantumSimulator into CommonNativeSimulator and QuantumSimulator (#853)
1 parent a66172d commit 76dddfd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+682
-440
lines changed

src/Simulation/Common/QubitManager.cs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,13 @@ public void Disable(IQArray<Qubit> qubitsToDisable)
321321
/// Allocates a qubit.
322322
/// Returns null if the qubit cannot be allocated.
323323
/// </summary>
324-
protected virtual Qubit? Allocate(bool usedOnlyForBorrowing)
324+
protected virtual Qubit Allocate(bool usedOnlyForBorrowing)
325325
{
326326
if (free == None)
327327
{
328328
if (!MayExtendCapacity)
329329
{
330-
return null;
330+
throw new NotEnoughQubits(1, this.FreeQubitsCount);
331331
}
332332

333333
long oldNumQubits = NumQubits;
@@ -398,12 +398,7 @@ public void Disable(IQArray<Qubit> qubitsToDisable)
398398
/// </summary>
399399
public Qubit Allocate()
400400
{
401-
Qubit? qb = Allocate(usedOnlyForBorrowing: false);
402-
if (qb == null)
403-
{
404-
throw new NotEnoughQubits(1, this.FreeQubitsCount);
405-
}
406-
return qb;
401+
return Allocate(usedOnlyForBorrowing: false);
407402
}
408403

409404
/// <summary>
@@ -429,15 +424,7 @@ public IQArray<Qubit> Allocate(long numToAllocate)
429424
}
430425
for (int i = 0; i < numToAllocate; i++)
431426
{
432-
Qubit? allocated = Allocate(usedOnlyForBorrowing: false);
433-
if (allocated == null)
434-
{
435-
for (int k = 0; k < i; k++)
436-
{
437-
Release(result[k], wasUsedOnlyForBorrowing: false);
438-
}
439-
throw new NotEnoughQubits(numToAllocate, this.FreeQubitsCount);
440-
}
427+
Qubit allocated = Allocate(usedOnlyForBorrowing: false);
441428
result.Modify(i, allocated);
442429
}
443430

@@ -594,15 +581,7 @@ internal IQArray<Qubit> Borrow(long numToBorrow, HashSet<Qubit> qubitsInUse)
594581
{ // Not enough qubits to borrow. Allocate what was not borrowed.
595582
for (long i = numBorrowed; i < numToBorrow; i++)
596583
{
597-
Qubit? allocated = Allocate(usedOnlyForBorrowing: true);
598-
if (allocated == null)
599-
{
600-
for (long k = numBorrowed; k < i; k++)
601-
{
602-
Release(borrowed[(int)k], wasUsedOnlyForBorrowing: true);
603-
}
604-
throw new NotEnoughQubits(numToBorrow, numBorrowed + this.FreeQubitsCount);
605-
}
584+
Qubit allocated = Allocate(usedOnlyForBorrowing: true);
606585
borrowed.Modify(i, allocated);
607586
}
608587
}

src/Simulation/Simulators/QuantumSimulator/ApplyControlledX.cs renamed to src/Simulation/Simulators/CommonNativeSimulator/ApplyControlledX.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
namespace Microsoft.Quantum.Simulation.Simulators
88
{
9-
public partial class QuantumSimulator
9+
public partial class CommonNativeSimulator
1010
{
1111
void IIntrinsicApplyControlledX.Body(Qubit control, Qubit target)
1212
{
1313
this.CheckQubits(new QArray<Qubit>(new Qubit[]{ control, target }));
1414

15-
MCX(this.Id, 1, new uint[]{(uint)control.Id}, (uint)target.Id);
15+
MCX(1, new uint[]{(uint)control.Id}, (uint)target.Id);
1616
}
1717
}
1818
}

src/Simulation/Simulators/QuantumSimulator/ApplyControlledZ.cs renamed to src/Simulation/Simulators/CommonNativeSimulator/ApplyControlledZ.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
namespace Microsoft.Quantum.Simulation.Simulators
88
{
9-
public partial class QuantumSimulator
9+
public partial class CommonNativeSimulator
1010
{
1111
void IIntrinsicApplyControlledZ.Body(Qubit control, Qubit target)
1212
{
1313
this.CheckQubits(new QArray<Qubit>(new Qubit[]{ control, target }));
1414

15-
MCZ(this.Id, 1, new uint[]{(uint)control.Id}, (uint)target.Id);
15+
MCZ(1, new uint[]{(uint)control.Id}, (uint)target.Id);
1616
}
1717
}
1818
}

src/Simulation/Simulators/QuantumSimulator/ApplyUncontrolledH.cs renamed to src/Simulation/Simulators/CommonNativeSimulator/ApplyUncontrolledH.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
namespace Microsoft.Quantum.Simulation.Simulators
88
{
9-
public partial class QuantumSimulator
9+
public partial class CommonNativeSimulator
1010
{
1111
void IIntrinsicApplyUncontrolledH.Body(Qubit target)
1212
{
1313
this.CheckQubit(target);
1414

15-
H(this.Id, (uint)target.Id);
15+
H((uint)target.Id);
1616
}
1717
}
1818
}

src/Simulation/Simulators/QuantumSimulator/ApplyUncontrolledRx.cs renamed to src/Simulation/Simulators/CommonNativeSimulator/ApplyUncontrolledRx.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
namespace Microsoft.Quantum.Simulation.Simulators
88
{
9-
public partial class QuantumSimulator
9+
public partial class CommonNativeSimulator
1010
{
1111
void IIntrinsicApplyUncontrolledRx.Body(double angle, Qubit target)
1212
{
1313
this.CheckQubit(target, nameof(target));
1414
CheckAngle(angle);
15-
R(this.Id, Pauli.PauliX, angle, (uint)target.Id);
15+
R(Pauli.PauliX, angle, (uint)target.Id);
1616
}
1717
}
1818
}

src/Simulation/Simulators/QuantumSimulator/ApplyUncontrolledRy.cs renamed to src/Simulation/Simulators/CommonNativeSimulator/ApplyUncontrolledRy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
namespace Microsoft.Quantum.Simulation.Simulators
88
{
9-
public partial class QuantumSimulator
9+
public partial class CommonNativeSimulator
1010
{
1111
void IIntrinsicApplyUncontrolledRy.Body(double angle, Qubit target)
1212
{
1313
this.CheckQubit(target, nameof(target));
1414
CheckAngle(angle);
15-
R(this.Id, Pauli.PauliY, angle, (uint)target.Id);
15+
R(Pauli.PauliY, angle, (uint)target.Id);
1616
}
1717
}
1818
}

src/Simulation/Simulators/QuantumSimulator/ApplyUncontrolledRz.cs renamed to src/Simulation/Simulators/CommonNativeSimulator/ApplyUncontrolledRz.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
namespace Microsoft.Quantum.Simulation.Simulators
88
{
9-
public partial class QuantumSimulator
9+
public partial class CommonNativeSimulator
1010
{
1111
void IIntrinsicApplyUncontrolledRz.Body(double angle, Qubit target)
1212
{
1313
this.CheckQubit(target, nameof(target));
1414
CheckAngle(angle);
15-
R(this.Id, Pauli.PauliZ, angle, (uint)target.Id);
15+
R(Pauli.PauliZ, angle, (uint)target.Id);
1616
}
1717
}
1818
}

src/Simulation/Simulators/QuantumSimulator/ApplyUncontrolledS.cs renamed to src/Simulation/Simulators/CommonNativeSimulator/ApplyUncontrolledS.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
namespace Microsoft.Quantum.Simulation.Simulators
88
{
9-
public partial class QuantumSimulator
9+
public partial class CommonNativeSimulator
1010
{
1111
void IIntrinsicApplyUncontrolledS.Body(Qubit target)
1212
{
1313
this.CheckQubit(target);
1414

15-
S(this.Id, (uint)target.Id);
15+
S((uint)target.Id);
1616
}
1717
}
1818
}

src/Simulation/Simulators/QuantumSimulator/ApplyUncontrolledSAdj.cs renamed to src/Simulation/Simulators/CommonNativeSimulator/ApplyUncontrolledSAdj.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
namespace Microsoft.Quantum.Simulation.Simulators
88
{
9-
public partial class QuantumSimulator
9+
public partial class CommonNativeSimulator
1010
{
1111
void IIntrinsicApplyUncontrolledSAdj.Body(Qubit target)
1212
{
1313
this.CheckQubit(target);
1414

15-
AdjS(this.Id, (uint)target.Id);
15+
AdjS((uint)target.Id);
1616
}
1717
}
1818
}

src/Simulation/Simulators/QuantumSimulator/ApplyUncontrolledSWAP.cs renamed to src/Simulation/Simulators/CommonNativeSimulator/ApplyUncontrolledSWAP.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Microsoft.Quantum.Simulation.Simulators
88
{
9-
public partial class QuantumSimulator
9+
public partial class CommonNativeSimulator
1010
{
1111
void IIntrinsicApplyUncontrolledSWAP.Body(Qubit qubit1, Qubit qubit2)
1212
{
@@ -16,9 +16,9 @@ void IIntrinsicApplyUncontrolledSWAP.Body(Qubit qubit1, Qubit qubit2)
1616

1717
this.CheckQubits(new QArray<Qubit>(new Qubit[]{ qubit1, qubit2 }));
1818

19-
MCX(this.Id, 1, new uint[]{(uint)qubit1.Id}, (uint)qubit2.Id);
20-
MCX(this.Id, 1, new uint[]{(uint)qubit2.Id}, (uint)qubit1.Id);
21-
MCX(this.Id, 1, new uint[]{(uint)qubit1.Id}, (uint)qubit2.Id);
19+
MCX(1, new uint[]{(uint)qubit1.Id}, (uint)qubit2.Id);
20+
MCX(1, new uint[]{(uint)qubit2.Id}, (uint)qubit1.Id);
21+
MCX(1, new uint[]{(uint)qubit1.Id}, (uint)qubit2.Id);
2222
}
2323
}
2424
}

0 commit comments

Comments
 (0)