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

Conversation

@DmitryVasilevsky
Copy link
Contributor

@DmitryVasilevsky DmitryVasilevsky commented Sep 26, 2021

Samuel Jaques contributed this sparse simulator code.
After removing some experimental code and unnecessary external components, this branch is ready to be pulled into a branch in qsharp-runtime repo.
Sparse simulator tests pass.
Once code is pulled into a branch in our repo, we will address the rest of the feedback.
Specifically some good feedback is still to be addressed from the initial PR: #767
PR will be merged without addressing entire feedback:
Some feedback is addressed, but the rest of the feedback will be addressed before merging to main.
It is much more convenient to address it in our repository.

sam-jaques and others added 30 commits July 5, 2021 18:42
…ck-3

Unit tests, comments and feedback from review
Copy link
Contributor

@thomashaener thomashaener left a comment

Choose a reason for hiding this comment

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

Thanks! I've added a few suggestions & comments.



~SparseSimulator() {
_execute_queued_ops();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this really necessary? If so, should make sure it doesn't throw.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It actually does throw right now as asserts may be queued...

Copy link
Contributor

@kuzminrobin kuzminrobin left a comment

Choose a reason for hiding this comment

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

As I understand this branch is just the latest state of the sam-jaques' PR. If that's the case then approving.
We just need to make sure this PR builds. After merging we can address all the feedback in our qsharp-runtime repo.

@DmitryVasilevsky
Copy link
Contributor Author

As I understand this branch is just the latest state of the sam-jaques' PR. If that's the case then approving.
We just need to make sure this PR builds. After merging we can address all the feedback in our qsharp-runtime repo.

Yes, this is the plan. We need it building. I will also address Thomas' feedback here.

@cgranade
Copy link
Contributor

Had a few more comments that would be good to handle before merging to main. In particular, we should make sure we have a plan for how to expose this simulator and its diagnostics to Python and IQ# users. Thanks!

Comment on lines +23 to +24
namespace Microsoft::Quantum::SPARSESIMULATOR
{
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] This file seems to use a mix of brace-on-same and brace-on-next code styles?

{
var result = Marshal.PtrToStringAnsi(Sample_cpp(Id)).ToCharArray();
Array.Reverse(result);
var bool_result = new bool[register.Length];
Copy link
Contributor

Choose a reason for hiding this comment

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

bool_result should be boolResult in C# style.

var bool_result = new bool[register.Length];
for (int i=0; i < register.Length; i++)
{
bool_result[i] = (result[register[i].Id] == '1' ? true : false);
Copy link
Contributor

Choose a reason for hiding this comment

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

You could probably use something like LINQ to write 348–353 out a bit easier:

return register.Select(qubit => result[qubit.Id] == '1' ? true : false).ToArray();


// Returns the amplitude of a specific state, given by a string representing its label
[DllImport(simulator_dll)]
private static extern void GetAmplitude_cpp(uint sim, uint label_length, char[] label, ref double real, ref double imag);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these be ref or out?

{
double real = 0;
double imag = 0;
GetAmplitude_cpp(Id, (uint)label.Length, label.ToCharArray(), ref real, ref imag);
Copy link
Contributor

Choose a reason for hiding this comment

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

If you define the GetAmplitude_cpp method with real and imag as out instead of ref, this wrapper method gets a bit easier:

GetAmplitude_cpp(Id, (uint)label.Length, length.ToCharArray(), out var real, out var image);
return new Microsoft.Quantum.Math.Complex((real, imag));


// It's expensive for the C++ simulator to separate two parts of a wavefunction,
// so if we just want the full state, it calls a different function
if (qubits == null)
Copy link
Contributor

Choose a reason for hiding this comment

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

What if qubits is not null, but consists of all currently allocated qubits?

@kuzminrobin
Copy link
Contributor

kuzminrobin commented Sep 28, 2021

@DmitryVasilevsky, in order to fix the build can you please apply the following change? This change excludes SparseSimulator form Simulation.sln so that the SparseSimulator is not built as part of Simulation.sln.

(I fail to push my change branch because I don't have access to Sam's repo, I didn't accept the permission form sam-jaques on time, and his permission has expired, so I requested again)

diff --git a/src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj b/src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj
index 73e63818..0869cee1 100644
--- a/src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj
+++ b/src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj
@@ -46,4 +46,11 @@
     </None>
   </ItemGroup>
 
+  <ItemGroup>
+    <BondCodegen Remove="SparseSimulator\**" />
+    <Compile Remove="SparseSimulator\**" />
+    <EmbeddedResource Remove="SparseSimulator\**" />
+    <QSharpCompile Remove="SparseSimulator\**" />
+  </ItemGroup>
+
 </Project>

@DmitryVasilevsky
Copy link
Contributor Author

DmitryVasilevsky commented Sep 28, 2021

Had a few more comments that would be good to handle before merging to main. In particular, we should make sure we have a plan for how to expose this simulator and its diagnostics to Python and IQ# users. Thanks!

@cgranade : This isn't going to main yet. This goes into a branch in our repo (from external contributor). We need to pull this code first and then fix it. I will address your comments after we pull the code but before it goes to main.

@DmitryVasilevsky DmitryVasilevsky merged commit ca33196 into microsoft:feature/sparse-simulator Sep 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants