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

Broken up QuantumSimulator into CommonNativeSimulator and QuantumSimulator #853

Merged
merged 16 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 5 additions & 26 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)
{
return null;
kuzminrobin marked this conversation as resolved.
Show resolved Hide resolved
throw new NotEnoughQubits(1, this.FreeQubitsCount);
}

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

/// <summary>
Expand All @@ -429,15 +424,7 @@ public IQArray<Qubit> Allocate(long numToAllocate)
}
for (int i = 0; i < numToAllocate; i++)
{
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);
}
Qubit allocated = Allocate(usedOnlyForBorrowing: false);
kuzminrobin marked this conversation as resolved.
Show resolved Hide resolved
result.Modify(i, allocated);
}

Expand Down Expand Up @@ -594,15 +581,7 @@ 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);
if (allocated == null)
{
for (long k = numBorrowed; k < i; k++)
{
Release(borrowed[(int)k], wasUsedOnlyForBorrowing: true);
}
throw new NotEnoughQubits(numToBorrow, numBorrowed + this.FreeQubitsCount);
}
Qubit allocated = Allocate(usedOnlyForBorrowing: true);
kuzminrobin marked this conversation as resolved.
Show resolved Hide resolved
borrowed.Modify(i, allocated);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would something like NativeSimulatorBase be a better name? Other simulators seem to inherit from it.

{
void IIntrinsicApplyControlledX.Body(Qubit control, Qubit target)
{
this.CheckQubits(new QArray<Qubit>(new Qubit[]{ control, target }));

MCX(this.Id, 1, new uint[]{(uint)control.Id}, (uint)target.Id);
MCX(1, new uint[]{(uint)control.Id}, (uint)target.Id);
kuzminrobin marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyControlledZ.Body(Qubit control, Qubit target)
{
this.CheckQubits(new QArray<Qubit>(new Qubit[]{ control, target }));

MCZ(this.Id, 1, new uint[]{(uint)control.Id}, (uint)target.Id);
MCZ(1, new uint[]{(uint)control.Id}, (uint)target.Id);
bettinaheim marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyUncontrolledH.Body(Qubit target)
{
this.CheckQubit(target);

H(this.Id, (uint)target.Id);
H((uint)target.Id);
bettinaheim marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyUncontrolledRx.Body(double angle, Qubit target)
{
this.CheckQubit(target, nameof(target));
CheckAngle(angle);
R(this.Id, Pauli.PauliX, angle, (uint)target.Id);
R(Pauli.PauliX, angle, (uint)target.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyUncontrolledRy.Body(double angle, Qubit target)
{
this.CheckQubit(target, nameof(target));
CheckAngle(angle);
R(this.Id, Pauli.PauliY, angle, (uint)target.Id);
R(Pauli.PauliY, angle, (uint)target.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyUncontrolledRz.Body(double angle, Qubit target)
{
this.CheckQubit(target, nameof(target));
CheckAngle(angle);
R(this.Id, Pauli.PauliZ, angle, (uint)target.Id);
R(Pauli.PauliZ, angle, (uint)target.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyUncontrolledS.Body(Qubit target)
{
this.CheckQubit(target);

S(this.Id, (uint)target.Id);
S((uint)target.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyUncontrolledSAdj.Body(Qubit target)
{
this.CheckQubit(target);

AdjS(this.Id, (uint)target.Id);
AdjS((uint)target.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyUncontrolledSWAP.Body(Qubit qubit1, Qubit qubit2)
{
Expand All @@ -16,9 +16,9 @@ void IIntrinsicApplyUncontrolledSWAP.Body(Qubit qubit1, Qubit qubit2)

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

MCX(this.Id, 1, new uint[]{(uint)qubit1.Id}, (uint)qubit2.Id);
MCX(this.Id, 1, new uint[]{(uint)qubit2.Id}, (uint)qubit1.Id);
MCX(this.Id, 1, new uint[]{(uint)qubit1.Id}, (uint)qubit2.Id);
MCX(1, new uint[]{(uint)qubit1.Id}, (uint)qubit2.Id);
MCX(1, new uint[]{(uint)qubit2.Id}, (uint)qubit1.Id);
MCX(1, new uint[]{(uint)qubit1.Id}, (uint)qubit2.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyUncontrolledT.Body(Qubit target)
{
this.CheckQubit(target);

T(this.Id, (uint)target.Id);
T((uint)target.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyUncontrolledTAdj.Body(Qubit target)
{
this.CheckQubit(target);

AdjT(this.Id, (uint)target.Id);
AdjT((uint)target.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyUncontrolledX.Body(Qubit target)
{
this.CheckQubit(target);

X(this.Id, (uint)target.Id);
X((uint)target.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyUncontrolledY.Body(Qubit target)
{
this.CheckQubit(target);

Y(this.Id, (uint)target.Id);
Y((uint)target.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
void IIntrinsicApplyUncontrolledZ.Body(Qubit target)
{
this.CheckQubit(target);

Z(this.Id, (uint)target.Id);
Z((uint)target.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
// `QSimAssert` makes an impression that it is never used,
// but since it inherits from Quantum.Diagnostics.AssertMeasurement
// (which is a C# class that corresponds to a Q# operation in our core libraries), it will be automatically used.
// It is instantiated via reflection, hence we don't see it easily in the code.
public class QSimAssert : Microsoft.Quantum.Diagnostics.AssertMeasurement
{
private QuantumSimulator Simulator { get; }
private CommonNativeSimulator Simulator { get; }

public QSimAssert(QuantumSimulator m) : base(m)
public QSimAssert(CommonNativeSimulator m) : base(m)
{
this.Simulator = m;
}
Expand All @@ -32,7 +36,7 @@ public QSimAssert(QuantumSimulator m) : base(m)
var tolerance = 1.0e-10;
var expectedPr = result == Result.Zero ? 0.0 : 1.0;

var ensemblePr = JointEnsembleProbability(this.Simulator.Id, (uint)paulis.Length, paulis.ToArray(), qubits.GetIds());
var ensemblePr = this.Simulator.JointEnsembleProbability((uint)paulis.Length, paulis.ToArray(), qubits.GetIds());

if (Abs(ensemblePr - expectedPr) > tolerance)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
public partial class CommonNativeSimulator
{
// `QSimAssertProb` makes an impression that it is never used,
// but since it inherits from Quantum.Diagnostics.AssertMeasurementProbability
// (which is a C# class that corresponds to a Q# operation in our core libraries), it will be automatically used.
// It is instantiated via reflection, hence we don't see it easily in the code.
public class QSimAssertProb : Microsoft.Quantum.Diagnostics.AssertMeasurementProbability
{
private QuantumSimulator Simulator { get; }
private CommonNativeSimulator Simulator { get; }

public QSimAssertProb(QuantumSimulator m) : base(m)
public QSimAssertProb(CommonNativeSimulator m) : base(m)
{
this.Simulator = m;
}
Expand All @@ -38,7 +42,7 @@ public QSimAssertProb(QuantumSimulator m) : base(m)
expectedPr = 1 - expectedPr;
}

var ensemblePr = JointEnsembleProbability(Simulator.Id, (uint)paulis.Length, paulis.ToArray(), qubits.GetIds());
var ensemblePr = this.Simulator.JointEnsembleProbability((uint)paulis.Length, paulis.ToArray(), qubits.GetIds());

if (Abs(ensemblePr - expectedPr) > tol)
{
Expand Down
Loading