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

Adds three OpenQASM samples; Q# to OpenQASM, OpenQASM to Q# and running Q#, by exporting OpenQASM on IBMQ #55

Closed
wants to merge 39 commits into from

Conversation

RolfHuisman
Copy link
Contributor

Adding a quick and dirty poc to show as an example that Q# will enable people to run their programs on the IBM Q machines as well. Proving my point that its an universal language.

…e people to run their programs on the IBM Q machines as well.
@msftclas
Copy link

msftclas commented May 9, 2018

CLA assistant check
All CLA requirements met.

@RolfHuisman
Copy link
Contributor Author

ApiKey was already been revoked, so its not an issue that its part of the commit.

open Microsoft.Quantum.Canon;

//Puts a qubit in superposition using the Hadamar gate and measures it
operation Hadamar () : (Result)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hadamard

mutable result = Zero; // set a mutable field to put the result in
using (qubits = Qubit[1]) //Create a Qubit register
{
H(qubits[0]); //Perform Hadamar on the Qubit,
Copy link
Contributor

Choose a reason for hiding this comment

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

same here: please replace with "Hadamard"

//You need to replace this with your own key from the quantum experience
var apiKey = "4616efdc29c9d4d751b3cd23a2e7d677ef8a6ff22b693afe0352e4f42b63e3a1135dc368484400b53cf7f4c8b96dcabf7cfc08442f2a1623614734c2e11e25f9";
var factory = new IbmQx4(apiKey); //Using different Factory
Console.WriteLine("Hadamar on IBMQx4");
Copy link
Contributor

Choose a reason for hiding this comment

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

same here: Hadamard

Console.WriteLine("Hadamar on IBMQx4");
for (int i = 0; i < 1; i++)
{
var result = Hadamar.Run(factory).Result;
Copy link
Contributor

Choose a reason for hiding this comment

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

and here again: Hadamard.Run

for (int i = 0; i < 1; i++)
{
var result = Hadamar.Run(factory).Result;
Console.WriteLine($"Result of hadamar is {result}");
Copy link
Contributor

Choose a reason for hiding this comment

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

Hadamard

@anpaz
Copy link
Member

anpaz commented May 10, 2018

Does it require Windows, or would it work on Mac/Linux? I don't see why it wouldn't.
The text in the README.md seems to imply that it only works on Windows.

@anpaz
Copy link
Member

anpaz commented May 10, 2018

All code files need to have License added.

@anpaz
Copy link
Member

anpaz commented May 10, 2018

It would be nice to have a README.md that explains installation and running instructions as well as a little explanation of what this sample does.

}
}

public override void StartOperation(string operationName, OperationFunctor functor, object inputValue)
Copy link
Member

Choose a reason for hiding this comment

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

This is supposed to be triggered on every operation, not only user defined ones. The fact that calling X or H doesn't trigger it is a bug, which has been fixed internally.
I don't think you want to put this on every call. I would suggest putting it on the constructor of the simulator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check, will be moving that.

{
get
{
throw new NotImplementedException();
Copy link
Member

Choose a reason for hiding this comment

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

I recommend removing all these NotImplemented operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair, where place holders for later. Will remove them in this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need to leave the measurement one in place though. Because of a reflection issue in the simulatorbase, if I leave it out the simulator thinks its not completely implemented.

{
return delegate (Qubit q1)
{
if (q1 == null)
Copy link
Member

Choose a reason for hiding this comment

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

doesn't this need to be logged in the quasm.log?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not yet used in the example. But indeed; Should have been for YXZ.

@martinquantum
Copy link
Contributor

martinquantum commented May 10, 2018 via email

{
return QVoid.Instance;
}
QuasmLog.AppendLine("h q[" + (uint)q1.Id + "];");
Copy link
Member

Choose a reason for hiding this comment

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

Interpolated strings IMHO would make it a bit easier to read, i.e.:
QuasmLog.AppendLine($"h q[{q1.Id}];");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would indeed make it more readable.

…. Fixed X gate, so implemented it for XYZ as well. Updated documentation and removed the bash workaround on windows, so it also should work on linux or mac.
…ple machines gave me different results. Included shots as well.
@qubeat
Copy link

qubeat commented May 11, 2018

I would suggest simply generate qasm file without try to send it to hardware. In such a case lot of problems disappear and even API key is not necessary (it may be ran on offline simulators, after all). Anyway it may not work for two-qubit gates without topology mapping information (taking into account connectivity and "direction" of C-NOT). Even for single qubit it is desirable to request availability and size of queue in device.

@RolfHuisman
Copy link
Contributor Author

RolfHuisman commented May 12, 2018

I agree with you that just generating the file instead of sending it to the hardware would be easier to implement and probably look a lot cleaner. This pull request is a reduced and cleaned up version of what I normally use in my demo's of Q#. The number one question I got after doing the demo's of Liquid (Q# predecessor) is showing this on the real hardware. People understood that the QASM file coud run on the machine, but having it inline had a different perception. Having said that, maybe its a good idea to split this into two samples. With a more fuller simulator that has a mapper and a more inline sample.

@qubeat
Copy link

qubeat commented May 12, 2018

Yes, I see. I was aware about LiquID, but only after reading Q# description realized that now it may be used with IBM QX quite naturally. But, the current device is anyway may not be controlled by user in "real-time" and qasm is desirable intermediate step. After that it may be used with different offline and online simulators or remapped to sent that into appropriate and currently available device. So I considered a working example with full set of Q# quantum gates, implemented at least for generating qasm file, as a desirable possibility. In fact, just before reading of your request I doubted, if to ask about that in an alternative site suggested as feedback pipeline. Such example would be useful also to clarify structure of QuantumSimulator and SimulatorBase stuff – I may not find a description anywhere. It may be useful also to show users how to do such a thing with other devices and simulators. E.g., I myself used in my old simulator maximally simple file format to save and read circuits and could try use Q# to generate that without necessity to write qasm parser and converter.

@RolfHuisman
Copy link
Contributor Author

Its currently mostly a sample to inspire, but I think it can be more. The current usage of SimulatorBase currently works, but I think I need to drop to the IAbstractFactory to get many of the more advanced constructs correct. The only thing that I'm currently going back and forward with myself over is where to and how put the mapper.

@qubeat
Copy link

qubeat commented May 12, 2018

Quite illustrative. Yet, I do not realize, how definitions in QSimR should look like - it would be necessary for most general one-qubit U3 gate on the chip.

…cation it generates. Also uses the allready defined samples instead of defining its own functions.
@RolfHuisman RolfHuisman changed the title Adding sample with running Q# on IBMQ Adds three OpenQASM samples; Q# to OpenQASM, OpenQASM to Q# and running Q#, by exporting OpenQASM on IBMQ May 24, 2018
@RolfHuisman
Copy link
Contributor Author

Curious to see why the linux build on vsts failed. Don't have the rights to see why. Suspect its because of something on the build environment.

@RolfHuisman
Copy link
Contributor Author

@anpaz-msft could you please do a re-review ? The old one isn't current anymore.

Copy link
Member

@anpaz anpaz left a comment

Choose a reason for hiding this comment

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

Looks pretty good on my end. Thanks for accommodating most comments. I do have one concern about StartOperation and EndOperation which I left as a comments. Thanks!

public abstract class OpenQasmDriver : SimulatorBase
{
private int operationDepth;
public override void StartOperation(string operationName, OperationFunctor functor, object inputValue)
Copy link
Member

Choose a reason for hiding this comment

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

The StartOperation and EndOperation are the only things I have a bit of concerned. On the upcoming version we will change the interface of these methods to receive different parameters, and the behavior will also change as today Primitive operations (like X or H) are not triggering these events, but now they will, and it feels to me this will break this code.
Could you instead use the constructor or the RunOpenQasm method instead?
If primitive operations triggering these methods is not a problem, we could leave it like this and I will just take care of updating the signature with the new version, or even better, we could just wait for the new version which should not take long.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the dependency on StartOperation and EndOperation and put it in the constructor. I put it in the RunOpenQasm, I will need to either wrap it all the time when there are multiple measurements in there, or check if it already has been added. Next version needs to measure more qubits, so a single action can be re-used.

</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Quantum.Canon" Version="0.2.1802.2202-preview" />
Copy link
Member

Choose a reason for hiding this comment

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

this one is unnecessary as you have a project reference below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed it.

Copy link
Member

@anpaz anpaz left a comment

Choose a reason for hiding this comment

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

Hey Rolf,
Thanks for the changes. I do have some minor comments but things look pretty nice!
One more request, though. You've added a new QASM->Q# translator, which is super-cool. Do you mind submitting that in a separate PR? If you could also add some unittests for it, that would be superb!

@@ -0,0 +1,2 @@
qiskit
IBMQuantumExperience
Copy link
Member

Choose a reason for hiding this comment

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

is there a difference between qiskit and IBMQuantumExperience? If so, we should add instructions on how to install qiskit in the readme, if not we should remove it.

Copy link
Member

Choose a reason for hiding this comment

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

You should also add a dependency on python3.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Qiskit is a dependency of the IBMQuantumExperience, so it gets automaticly installed when installing the Quantum Experience.

{
try
{
var directory = "Qiskit";
Copy link
Member

Choose a reason for hiding this comment

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

Maybe another name, or maybe save this into the "obj" directory?

Copy link
Member

Choose a reason for hiding this comment

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

More over, looks like QiskitInterface.py expects the file to be in the same folder, so if you try to do:
dotnet run
from the Samples\Qiskit directory, it doesn't work. I think you probably want to just write/read everything from the current folder.

var output = Path.Combine(directory, "output.txt");

//Change to unix compatible file format
File.WriteAllText(input, quasm.ToString().Replace("\r\n", "\n"), Encoding.ASCII);
Copy link
Member

Choose a reason for hiding this comment

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

Before trying to write, you should check if the directory exists and create it if necessary, otherwise things fail.

@RolfHuisman
Copy link
Contributor Author

Split up in #59 and #58, so closing this one.

jwittner pushed a commit to jwittner/Quantum that referenced this pull request Dec 16, 2019
jwittner pushed a commit to jwittner/Quantum that referenced this pull request Dec 16, 2019
)

* Updating QDK to latest version. (microsoft#53)

* Fixing doc gen call (microsoft#39)

* fixing doc gen call

* adding a test for docs generation

* need to generate docs for source files only such that we can build the docs for muliple dlls without interferences

* forgot to check if the dictionary exists before clearning it in test

* Updating qdk to latest version

* Fixing vs code extension build script on linux (microsoft#56)

* fixing a stackoverflow for large files (microsoft#55)

* Build VS Code dependencies from either pwsh or powershell. (microsoft#59)

* Add automatic indentation to Visual Studio extension (microsoft#60)

* Add automatic indentation to Visual Studio extension

* Find the indentation of the last non-empty line

The previous line can be empty, which would reset the indentation to 0 if e.g. you pressed enter twice in a row. Loop backwards to find the last non-empty line and use that line's indentation instead.

* Remove assumption from GetLastNonEmptyLine

* Move QsSmartIndentProvider to the top of the file

* Flip order of ternary operator in GetIndentation

* Add doc comment to GetDesiredIndentation

* Add note about nullable return type of GetDesiredIndentation

* Show signature help for the first argument (microsoft#63)

We also trigger signature help on "("

* Always log exceptions from file queries in debug mode (microsoft#70)

* Handling specialization redeclarations (microsoft#85)

* Add install template and new project commands to VS Code extension. (microsoft#79)

* Compile time errors for overflowing int and double literals (microsoft#87)
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.

5 participants