Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
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
11 changes: 5 additions & 6 deletions src/Simulation/CSharpGeneration/SimulationCode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ module SimulationCode =
[ ``public`` ]
``{``
[
(``ident`` "this") <|.|> (``ident`` "Gate") <-- (``ident`` "m") |~> ("IGate_" + name) |> statement
(``ident`` "this") <|.|> (``ident`` "Impl") <-- (``ident`` "m") |> statement
]
``}``
:> MemberDeclarationSyntax
Expand Down Expand Up @@ -1041,7 +1041,6 @@ module SimulationCode =
| _ -> op.ArgumentTuple
let argName, argsInit = getInputVarWithInit args
let specCall =
(userDefinedName None op.FullName.Name) + "__" +
match sp.Kind with
| QsBody -> ""
| QsAdjoint -> "Adjoint"
Expand All @@ -1052,7 +1051,7 @@ module SimulationCode =
let rec argsToVars = function
| QsTupleItem one -> [one.VariableName |> name]
| QsTuple many -> many |> Seq.map argsToVars |> List.concat
let callExp = (``ident`` "Gate") <.> (``ident`` specCall, argsToVars args)
let callExp = ``((`` (``cast`` ("IIntrinsic" + (userDefinedName None op.FullName.Name)) (``ident`` "Impl")) ``))`` <.> (``ident`` specCall, argsToVars args)
Copy link
Collaborator Author

@swernli swernli Feb 26, 2021

Choose a reason for hiding this comment

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

@cgranade With this change, a simulator that tries to invoke a gate for which it doesn't implement the interface will get an invalid cast error instead of a null dereference. Is that sufficient to address #513 ?

let statements =
match sp.Signature.ReturnType.Resolution with
| QsTypeKind.UnitType ->
Expand Down Expand Up @@ -1278,8 +1277,8 @@ module SimulationCode =
``get`` (``=>`` (``literal`` name) )
:> MemberDeclarationSyntax

let buildGate name =
``propg`` ("IGate_" + name) "Gate" [ ``private``; ``protected`` ]
let buildImpl name =
``propg`` "IOperationFactory" "Impl" [ ``private``; ``protected`` ]
:> MemberDeclarationSyntax

let buildFullName (name : QsQualifiedName) =
Expand Down Expand Up @@ -1461,7 +1460,7 @@ module SimulationCode =
yield buildFullName context.current.Value
if globalContext.entryPoints |> Seq.contains op.FullName then
yield buildOperationInfoProperty globalContext inType outType nonGenericName
if isConcreteIntrinsic then yield buildGate name
if isConcreteIntrinsic then yield buildImpl name
yield! opProperties
]

Expand Down
1 change: 0 additions & 1 deletion src/Simulation/QSharpCore/Intrinsic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public partial class ResetAll
}
}


[Obsolete("This class is deprecated and will be removed in a future release. Considering using the corresponding callable class from 'Microsoft.Quantum.Intrinsic' directly.")]
public class QSimExp : Exp
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ namespace Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators.Implementati
{
using System;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

public partial class QCTraceSimulatorImpl
{
public virtual Result Measure__Body(IQArray<Pauli> paulis, IQArray<Qubit> targets)
Result IIntrinsicMeasure.Body(IQArray<Pauli> paulis, IQArray<Qubit> targets)
{
return this.Measure(paulis, targets);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators.Implementati
/// <summary>
/// Internals of <see cref="QCTraceSimulator"/>. For internal use only.
/// </summary>
public partial class QCTraceSimulatorImpl : SimulatorBase, IGate_Measure
public partial class QCTraceSimulatorImpl : SimulatorBase, IIntrinsicMeasure
{
protected readonly QCTraceSimulatorConfiguration configuration;
private readonly QCTraceSimulatorCore tracingCore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
{
public virtual void ApplyUncontrolledRx__Body(double angle, Qubit target)
void IIntrinsicApplyUncontrolledRx.Body(double angle, Qubit target)
{
this.CheckQubit(target, nameof(target));
CheckAngle(angle);
R(this.Id, Pauli.PauliX, angle, (uint)target.Id);
}

public virtual void ApplyUncontrolledRx__AdjointBody(double angle, Qubit target)
void IIntrinsicApplyUncontrolledRx.AdjointBody(double angle, Qubit target)
{
ApplyUncontrolledRx__Body(-angle, target);
((IIntrinsicApplyUncontrolledRx)this).Body(-angle, target);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
{
public virtual void ApplyUncontrolledRy__Body(double angle, Qubit target)
void IIntrinsicApplyUncontrolledRy.Body(double angle, Qubit target)
{
this.CheckQubit(target, nameof(target));
CheckAngle(angle);
R(this.Id, Pauli.PauliY, angle, (uint)target.Id);
}

public virtual void ApplyUncontrolledRy__AdjointBody(double angle, Qubit target)
void IIntrinsicApplyUncontrolledRy.AdjointBody(double angle, Qubit target)
{
ApplyUncontrolledRy__Body(-angle, target);
((IIntrinsicApplyUncontrolledRy)this).Body(-angle, target);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
{
public virtual void ApplyUncontrolledRz__Body(double angle, Qubit target)
void IIntrinsicApplyUncontrolledRz.Body(double angle, Qubit target)
{
this.CheckQubit(target, nameof(target));
CheckAngle(angle);
R(this.Id, Pauli.PauliZ, angle, (uint)target.Id);
}

public virtual void ApplyUncontrolledRz__AdjointBody(double angle, Qubit target)
void IIntrinsicApplyUncontrolledRz.AdjointBody(double angle, Qubit target)
{
ApplyUncontrolledRz__Body(-angle, target);
((IIntrinsicApplyUncontrolledRz)this).Body(-angle, target);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

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

S(this.Id, (uint)target.Id);
}

public virtual void ApplyUncontrolledS__AdjointBody(Qubit target)
void IIntrinsicApplyUncontrolledS.AdjointBody(Qubit target)
{
this.CheckQubit(target);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
{
public virtual void ApplyUncontrolledSWAP__Body(Qubit qubit1, Qubit qubit2)
void IIntrinsicApplyUncontrolledSWAP.Body(Qubit qubit1, Qubit qubit2)
{
// Issue #44 (https://github.com/microsoft/qsharp-runtime/issues/44)
// If/when the simulator provides access to the accelerated SWAP functionality,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

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

T(this.Id, (uint)target.Id);
}

public virtual void ApplyUncontrolledT__AdjointBody(Qubit target)
void IIntrinsicApplyUncontrolledT.AdjointBody(Qubit target)
{
this.CheckQubit(target);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

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

Expand Down
15 changes: 8 additions & 7 deletions src/Simulation/Simulators/QuantumSimulator/Exp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

using System;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

namespace Microsoft.Quantum.Simulation.Simulators
{
public partial class QuantumSimulator
{
public virtual void Exp__Body(IQArray<Pauli> paulis, double angle, IQArray<Qubit> targets)
void IIntrinsicExp.Body(IQArray<Pauli> paulis, double angle, IQArray<Qubit> targets)
{
this.CheckQubits(targets);
CheckAngle(angle);
Expand All @@ -21,12 +22,12 @@ public virtual void Exp__Body(IQArray<Pauli> paulis, double angle, IQArray<Qubit
Exp(this.Id, (uint)paulis.Length, paulis.ToArray(), angle, targets.GetIds());
}

public virtual void Exp__AdjointBody(IQArray<Pauli> paulis, double angle, IQArray<Qubit> targets)
void IIntrinsicExp.AdjointBody(IQArray<Pauli> paulis, double angle, IQArray<Qubit> targets)
{
Exp__Body(paulis, -angle, targets);
((IIntrinsicExp)this).Body(paulis, -angle, targets);
}

public virtual void Exp__ControlledBody(IQArray<Qubit> controls, IQArray<Pauli> paulis, double angle, IQArray<Qubit> targets)
void IIntrinsicExp.ControlledBody(IQArray<Qubit> controls, IQArray<Pauli> paulis, double angle, IQArray<Qubit> targets)
{
this.CheckQubits(controls, targets);
CheckAngle(angle);
Expand All @@ -37,13 +38,13 @@ public virtual void Exp__ControlledBody(IQArray<Qubit> controls, IQArray<Pauli>
}

SafeControlled(controls,
() => Exp__Body(paulis, angle, targets),
() => ((IIntrinsicExp)this).Body(paulis, angle, targets),
(count, ids) => MCExp(this.Id, (uint)paulis.Length, paulis.ToArray(), angle, count, ids, targets.GetIds()));
}

public virtual void Exp__ControlledAdjointBody(IQArray<Qubit> controls, IQArray<Pauli> paulis, double angle, IQArray<Qubit> targets)
void IIntrinsicExp.ControlledAdjointBody(IQArray<Qubit> controls, IQArray<Pauli> paulis, double angle, IQArray<Qubit> targets)
{
Exp__ControlledBody(controls, paulis, -angle, targets);
((IIntrinsicExp)this).ControlledBody(controls, paulis, -angle, targets);
}
}
}
7 changes: 4 additions & 3 deletions src/Simulation/Simulators/QuantumSimulator/H.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
// Licensed under the MIT License.

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Intrinsic.Interfaces;

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

H(this.Id, (uint)target.Id);
}

public virtual void H__ControlledBody(IQArray<Qubit> controls, Qubit target)
void IIntrinsicH.ControlledBody(IQArray<Qubit> controls, Qubit target)
{
this.CheckQubits(controls, target);

SafeControlled(controls,
() => H__Body(target),
() => ((IIntrinsicH)this).Body(target),
(count, ids) => MCH(this.Id, count, ids, (uint)target.Id));
}
}
Expand Down
Loading