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

Commit b30286c

Browse files
authored
Preparing the QuantumSimulator for unifying the Dump(). (#867)
* Moved some classes from IQSharp
1 parent 5ea517c commit b30286c

File tree

7 files changed

+350
-54
lines changed

7 files changed

+350
-54
lines changed

src/Simulation/Core/IOperationFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface IOperationFactory
2424
/// <summary>
2525
/// Returns an instance of the requested operation.
2626
/// </summary>
27-
I Get<I>(Type T);
27+
I Get<I>(Type t);
2828

2929
/// <summary>
3030
/// Returns an instance of the requested operation.

src/Simulation/Native/src/simulator/capi.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ extern "C"
2222
MICROSOFT_QUANTUM_DECL void destroy(_In_ unsigned sid); // NOLINT
2323
MICROSOFT_QUANTUM_DECL void seed(_In_ unsigned sid, _In_ unsigned s); // NOLINT
2424
MICROSOFT_QUANTUM_DECL void Dump(_In_ unsigned sid, _In_ bool (*callback)(size_t, double, double));
25+
26+
// TODO(rokuzmin): What does it return?
2527
MICROSOFT_QUANTUM_DECL bool DumpQubits(
2628
_In_ unsigned sid,
2729
_In_ unsigned n,

src/Simulation/Native/src/simulator/simulatorinterface.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class SimulatorInterface
9999
{
100100
assert(false);
101101
}
102+
// TODO(rokuzmin): What does it return?
102103
virtual bool dumpQubits(std::vector<logical_qubit_id> const& qs, bool (*callback)(size_t, double, double))
103104
{
104105
assert(false);

src/Simulation/Simulators/QuantumSimulator/Dump.cs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,29 @@ protected virtual QVoid Dump<T>(T target, IQArray<Qubit>? qubits = null)
2323
{
2424
var filename = (target is QVoid) ? "" : target.ToString();
2525

26-
QVoid process(Action<string> channel)
27-
{
28-
var ids = qubits?.Select(q => (uint)q.Id).ToArray() ?? QubitIds;
29-
30-
var dumper = new SimpleDumper(this, channel);
31-
channel($"# wave function for qubits with ids (least to most significant): {string.Join(";", ids)}");
32-
33-
if (!dumper.Dump(qubits))
34-
{
35-
channel("## Qubits were entangled with an external qubit. Cannot dump corresponding wave function. ##");
36-
}
37-
38-
return QVoid.Instance;
39-
}
40-
41-
var logMessage = this.Get<ICallable<string, QVoid>, Microsoft.Quantum.Intrinsic.Message>();
42-
43-
// If no file provided, use `Message` to generate the message into the console;
26+
// If no file provided, output to the console;
4427
if (string.IsNullOrWhiteSpace(filename))
4528
{
46-
var op = this.Get<ICallable<string, QVoid>, Microsoft.Quantum.Intrinsic.Message>();
47-
return process((msg) => op.Apply(msg));
29+
new DisplayableStateDumper(this).Dump(qubits);
4830
}
4931
else
5032
{
5133
try
5234
{
53-
using (var file = new StreamWriter(filename))
54-
{
55-
return process(file.WriteLine);
56-
}
35+
using var file = new StreamWriter(filename);
36+
new DisplayableStateDumper(this, file.WriteLine).Dump(qubits);
5737
}
5838
catch (Exception e)
5939
{
40+
var logMessage = this.Get<ICallable<string, QVoid>, Microsoft.Quantum.Intrinsic.Message>();
6041
logMessage.Apply($"[warning] Unable to write state to '{filename}' ({e.Message})");
61-
return QVoid.Instance;
6242
}
6343
}
44+
return QVoid.Instance;
6445
}
6546

66-
public class QsimDumpMachine<T> : Quantum.Diagnostics.DumpMachine<T>
67-
{
47+
public class QsimDumpMachine<T> : Quantum.Diagnostics.DumpMachine<T> // Is inherited (and replaced at runtime)
48+
{ // by (iqsharp's) JupyterDumpMachine<T>.
6849
private QuantumSimulator Simulator { get; }
6950

7051
public QsimDumpMachine(QuantumSimulator m) : base(m)
@@ -80,8 +61,8 @@ public QsimDumpMachine(QuantumSimulator m) : base(m)
8061
};
8162
}
8263

83-
public class QSimDumpRegister<T> : Quantum.Diagnostics.DumpRegister<T>
84-
{
64+
public class QSimDumpRegister<T> : Quantum.Diagnostics.DumpRegister<T> // Is inherited (and replaced at runtime)
65+
{ // by (iqsharp's) JupyterDumpRegister<T>.
8566
private QuantumSimulator Simulator { get; }
8667

8768
public QSimDumpRegister(QuantumSimulator m) : base(m)

src/Simulation/Simulators/QuantumSimulator/NativeImports.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public partial class QuantumSimulator
8686
[DllImport(QSIM_DLL_NAME, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, EntryPoint = "Dump")]
8787
private static extern void sim_Dump(uint id, DumpCallback callback);
8888

89+
// TODO(rokuzmin): What does it return?
8990
[DllImport(QSIM_DLL_NAME, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, EntryPoint = "DumpQubits")]
9091
private static extern bool sim_DumpQubits(uint id, uint cout, uint[] ids, DumpCallback callback);
9192

src/Simulation/Simulators/QuantumSimulator/SimulatorBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ public SimulatorBase(IQubitManager? qubitManager = null, int? seed = null)
9393
}
9494
}
9595

96-
public I Get<I>(Type T)
96+
public I Get<I>(Type t)
9797
{
98-
return (I)this.GetInstance(T);
98+
return (I)this.GetInstance(t);
9999
}
100100

101101
/// <summary>

0 commit comments

Comments
 (0)