How To Contribute To Qiskit — Programming on Quantum Computers Ep 7

How To Contribute To Qiskit — Programming on Quantum Computers Ep 7


– The issues are a great
place to start contributing and in particular, if this is
your first time developing, you might also find it a good place to start by clicking this
Good First Issue label. (upbeat pop music) (upbeat pop music) (upbeat pop music) (upbeat pop music) Hey everyone! Welcome back to Coding
With Qiskit, this is Abe. So in today’s episode, we’re going to be making
our first contribution to the open-source software, Qiskit. The idea here is that we’re going to look through the issues in Qiskit, in particular, in a component of Qiskit called Qiskit Terra and among these issues, we’re going to pick one interesting one and find possible solutions to it. Once we’ve identified a solution, we’re going to submit that solutions back to the open-source software and in this way, we’re
going to have become official contributors to
the open-source software. This is a meaningful
contribution that we’re making because it impacts not just the software but the entire Quantum
computing community. So in this video, we’ll walk through all of
these steps individually. We’ll download Qiskit, we will find the solutions on our computer by navigating through the code, and eventually we’ll
go through the process of submitting changes to
the code back to get help. So, let’s get started! So as we’ve mentioned before, Qiskit does open-source software and what that means is
you have the ability to contribute to it and even fix issues and contribute to the
development of Quantum Software. So what I’m going to do here is start off first at
the Qiskit.org webpage. Here I have my browser open. I’m going to go to the
GitHub page for Qiskit. And GitHub is where the
open-source software lives. When you go to this GitHub page, you’ll see a few pinned repositories and a list of other repositories. And we’re going to start off by looking at the four elements of Qiskit, so Terra, Ignis, Aqua, and Aer. And the idea here is that
Terra is the foundational layer and this is where we’ve
been building circuits in previous videos. This is how we’ve been executing things. This Terra foundation layer is where we’ll be focusing on today. What I want to do is show
you how we can fix an error, work through an error with you live, and submit a change to the code so that we fix that error in Qiskit Terra. So the best place to start is by clicking this
Qiskit Terra repository and looking here in the issues. The issues are a great
place to start contributing and in particular, if this is
your first time developing, you might also find it
a good place to start by clicking this good first issue label. When you do that, now all
the issues that you’re seeing are those that have the good first issue tag applied to them. So if you don’t find anything that says good first issue to click on, you can also just type
this in the search box. So you can just say is issue “is:open” and the label is “good first issue”, and then hit search. So these good first issues
are great to work on and to get started contributing to Qiskit and today what I want to do is work through one of them with you. So looking down the list, you can see a list of ones to work on. So let’s say maybe this one. So the idea here is that
someone has reported this issue and the issue says there
is an inconsistency in how the parameters of the gate are showed in the circuit plot. So it looks like this code, which by now you should be familiar with, is adding a u3 gate and a cu3 gate and is drawing them. So this is the output
of the drawing function and you can see the person is reporting the inconsistency
between how these numbers don’t have any parenthesis
around them, but these ones do. Okay, so the issue we have to fix is figuring out why
there’s this inconsistency. The person is saying, “If I have to choose, I
would go with removing the parenthesis everywhere. But for sure, it should be everywhere the same.” Okay, mmhmm. So the person is reporting
another inconsistency so he’s now saying “circuit_all.draw(output=mpl” and it looks like if you’re
passing the argument, use pi format, you get pi’s here, but you
get nothing on this gate. If you don’t use pi, you get back the previous plot, but you have this inconsistency
with the parenthesis. Okay, so it looks like generally, we’re going to be trying
to fix this problem. One good habit to have is to look through what progress
has been made so far. So someone here commented
that they’re interested in trying to solve. Before shooting this video, I took a look at the issues and decided to maybe try
and work on this one. So I asked for permission to work on this and I said, “How’s this going?” and once I got permission to go ahead, I’m going to be working on this. So, it’s just a good
idea to make sure that you’re contributing but you’re also not stepping on other people’s toes. Let’s go ahead with solving this issue. How do we start? How do we begin? So what I’m going to do is first begin by trying to modify this code, right? So somehow we need to get
this code onto our computer. So I’m going to go to the
Qiskit Terra repository and then I’m going to fork
it into my personal GitHub. So I click the fork link
and I say to my own profile. And once this fork is complete, I’ll have a copy of the
code into my own profile. As you can see now, the GitHub page is my
profile/Qiskit-Terra. So this branch that I’m
working on in my own fork is even with master and from here, I can continue with trying
to work from my own computer. Okay so once we forked the repository to our own personal GitHub, then we click the clone download link and copy over the link
to clone this repository and in our computer, we go to terminal and type, “git clone” and bring in the address
for the repository. Keep in mind here that the
address that I’m pasting here is from my own personal Git page. Once the cloning is complete, if you look at the directory, there should be a folder
called Qiskit Terra. Let’s change into that folder. So I’m going to say, “cd qiskit terra” and I’m going to say, “git status”. So you see, our branch is on master and we’re up to date with origin master. And in particular, what I’m going to do is say, “git remote -v” and look at where origin points. So origin of this folder points to the repository where we did our fork. There’s also an upstream, so I’m going to add the Qiskit repository from which I forked to upstream here. So I’m going to say,
“git remote add upstream” and then I’m going to bring in
the address of Qiskit Terra, copy this particular
link, and paste it here. Notice that this time, the upstream is point to the
original Qiskit repository. Once you do that, then
say, “git remote -v” and you should see that
the origin is pointing to my personal repository and the upstream is pointing
to the Qiskit repository. It’s a good idea also to work on branches, so what I’m going to do is say, “git branch” and I’m
going to say the branch is going to be, “fixissue” and if I remember correctly, the issue number is 2822. So I’m going to say the
name of the branch is, “fixissue2822branch” and then I’m going to say, “git checkout fixissue2822branch” and that means I’ve
changed over to this branch that I just created and I can say, “git status” and you see I’m on this branch now, instead of previously we were on master when we started this work. All right, now we’re
ready to start working. So, in order to see any changes that I’m making in the code, what I like to do is
work in an environment for Python that’s different. So I’m going to also
create a new environment. So to create the new environment, I’m going to go to
Qiskit.org/documentation and I’m going to follow the instructions in the installing page and building Qiskit packages from source. So we’ll put a link to this page in the description as well. But the link that you’re looking for, the instructions that
you’re looking for are here. So, “conda create -y -n QiskitDevenv”. So that’s the instruction that we’re going to
type into our terminal. So I’m going to say, “conda create -y -n”. I’m going to change the
name of the environment to, “fixissueenviornment”. And I’m going to say, “python=3” and Conda goes ahead and
creates that environment. And now we can activate that environment. You can simply paste the instruction here. “conda activate fixissueenviornment” and now you see this changes from base to the fix issue environment. The next step is to install
Qiskit Terra from source. So we are already in the
Qiskit Terra directory. So the instruction to do that is also on this page in our browser. So we’re going to go down to this section, which says, “Install Terra from source” and we’re going to run
this list of things. So, in particular, we’ve
done our own clone, so we’re going to ignore this line. We’re changed into the directory
already so I’m just going to start from this
particular line which says, “pip install cython”. That successfully installed. And then I’m going to say, “pip install -r requirementsdev”. And once that completes, I’m going to go to the
editable mode instructions and do, “pip install -e .”. So, “pip install -e .”. So the -e tag here,
allows us to install pip in editable mode, which means we can edit the code and we don’t have to do
this installation over and over again in order to see changes from the code reflecting in our work. Okay, and once that completes, now we have Terra installed in this particular environment called, “fix issue environment” and now we’re ready to start working. So what I’m going to do
is quickly double check that Terra is installed
so I’ll say, “python”. You can see that there’s Python 3.7.4 in my environment and what I’m going to do is say, “import Qiskit” and run that line. And you see we get an error that says, “There’s no IBMQ provider and
there’s not Qiskit error”. That’s fine because we haven’t installed these particular packages but it looks like Terra
itself is installed. So again, what I’m going to do is say, “qiskit.__qiskit_version__” and that tells me that I
have this Terra version, which is the development branch and I have none of the other packages, which is exactly what we expect, given that we just installed Terra only. So lets think about where we are now. So we have forked the
repository from Qiskit to our own GitHub profile, and then we have downloaded
that code onto our computer by cloning using GitHub, and what else have we done? We also have installed
this particular version of Qiskit Terra that we’ve downloaded into this particular
environment that we’ve created. Okay so what does that mean? Now we’re ready to start running things with this version of Qiskit Terra. So I personally like to write
code in Jupyter Notebooks. So what that means is now I need to launch Jupyter
Notebooks and start writing code. What I’m going to do to keep things clean is to also create a kernel
on which we’ll run code in the Jupiter Notebooks. So the instructions to create a new kernel are available from this page and we will also create a link to this in the description below. So if you scroll down to kernels for different
environments section of this page and you use this particular instruction, this is the one that I’m going
to use in my command line. So I’m going to copy this over. I’m going to exit out
of this Python instance. So right now I’m in the
Qiskit Terra folder. I’m going to go one above it and then I’m going to paste in that piece of code from the web. And I’m going to say the
name of the environment is, “fixissueenviornment” and the name is again going to come here. So this is the name of
the environment that I’m installing the kernel in and this is the display name that will be seen in the Jupyter Notebook. So type that in and we
have the kernel installed. So what I’m going to do then is just say, “jupyter notebook”. So that creates a new Jupyter instance and what I can then say is, “new fixissueenviornment”, so this is the one that we just created. I’m going to create a new Jupyter Notebook and this is the kernel
that’s going to run. So let’s do the same thing as before. I’m going to say, “import
qiskit”, hit enter. Again we got the same errors that we did– that we got on the command line, which is we need to install Qiskit Aer and Qiskit IBMQ provider and if we say,
“qiskit.__qiskit_version__”, we get the version of Terra and nothing else is installed. So far, so good. So now we are able to write
code using this particular version of Terra in
this particular kernel. All right so now, let’s go back to the issue
that we were about to work on. So the issue was in the good first issues, I think it was number 2822. Yep. So this is the piece of code that the person has reported, that’s creating the circuit. So let’s copy that over
into our Jupyter Notebook and see what happens when we run it. So as soon as we run it,
we get an error, right? So it says name quantum
register is not defined. So that’s because we haven’t done from Qiskit import everything. If we do that, now we get a new error which says name pi is not defined and this error looks like it’s coming because the person has not said, “import numpy as np” and then said, “pi=np.pi”. So now when we run this code, we’re able to get the
print out without errors and it looks like this issue
is showing up now, right? So we see pi, pi, pi, and we see nothing in this gate. Both gates are taking
in the same parameters for the angles so it looks
like there’s some issue that’s cause the single cubic gates not to get the print out of the angles, but the two cubic gates seem to be fine. Okay, so my instinct when
I see something like this is to first see if it’s localized to just a matplotlib plot or to all kinds of circuit plots. So, I’m going to copy this code over and just remove this argument and run it. So now this is the ascii
draw version of the circuit. So you see here, the angles are
showing fine for both gates. So it looks like this issue is limited to the matplotlib version of the code. Okay, that’s a hint as to
what the problem might be. So now what I want to know is
how this function.draw works so that I can see how it
does things without arguments and with arguments. So I’m going to take this and I’m going to ask for help on it. So one of the best things about Qiskit is that there are docstrings and they’re very helpful
in these functions. So if you just ask for help, you should see something
that’s very useful. So you see here, there’s
something that says, “Help on TextDrawing in module
qiskit.visulalization.text” Okay so, this thing takes arguments
and gives these outputs so let’s look in this file, qiskit.visualization.text. So what I’m going to do
is open a finder window and navigate to this
particular piece of code. So this is the directory
where we’re working now, so this is the Qiskit Terra
folder that was cloned and looking inside we’re looking for Qiskit, visualization, texts. So I’ll go to Qiskit, I’ll go to visualization, and I’ll look in the text code. So what I’m seeing here is something that draws the text code. Okay, so these are all the details that are involved in
drawing the Quantum circuit and it’s probably fascinating
to look through all of this. What I’m going to do is ask
for where the NPL line exists. Nowhere. All right. Looking in the same folder, we have something for plotting and text. The matplotlib plot that
I used probably uses this particular file called matplotlib so let’s open that here as well. Look inside here. (upbeat pop music) (upbeat pop music) Okay, so this looks promising. So, this looks like the fragment of code that draws single cubic gates and looks like we have
multiple cubic gates as well. Let’s look in the single
cubic k agates code. Ahh, okay. I think me might have found our issue. So, I’m seeing self.gate here and there’s text that displays
what kind of gate it is. So this is probably the name of the gate and then there’s subtext. I think these are the pi, pi, pi arguments that we’re supposed to be trying. So let’s make sure first of all. So let’s print PRN and just save that very quickly. It looks like this is length less than 20 is when this shows. If length is bigger than 20, then this section of code
runs without subtext. So let’s also see what the length of PRM is that we’re trying to work with. So I’m going to say
print the length as well. So I’m going to then go back and restart the kernel
and run everything again. Okay! So we are in the right
section of code at least. So this is the PRM parameter
and it’s length is 23. Okay, I think now we know
what the error is, right? So, if the length is less than 20, we show the subtext. Otherwise, we don’t show the subtext. So if I make this number
something like maybe, 28, I don’t know, and
rerun the piece of code, then we should see the parameters, right? – [Student] Sounds correct. – [Instructor] Ahh, there we go! Okay so, now we get the numbers to show up so we fixed at least that part. In terms of consistency, there is parenthesis here and none here. So that’s probably because of this, so let’s remove this
and go back to the code, kernel, restart, and run all. – [Student] Sounds Correct. – [Instructor] Yeah, now
we have no parenthesis and it looks like things are consistent. So that fixes the issue that was reported. So, one more thing I’m going to do is, this number 28 is something
I just randomly picked. Let’s make sure that we can display something like pi over two, pi over two, and pi over two. So, we’re going to do that and hit cell, run all. So it looks like 28 is able to accommodate this because 25 is the length
when you have one digit in all the denominators. Okay, so 25 is a reasonable number. I’m going to say 28 to be safe. I’m going to remove these print statements because I inserted them for debugging. And now we’re ready to report
that we fixed this issue. So one last check. I’m going to restart the
kernel and run everything. No print outs for debugging and we get the consistencey that we were looking for at first. All right, at this point, we are ready to now submit this code back to GitHub and report
that we fixed the issue. So to be safe, one of the
things that we have to do is make sure that we
haven’t broken anything in the process of fixing an issue. So we’re going to run a few tests. So I’m going to go back
to the command line and go back to the
directory where I’m working. So I’m going to activate the fix issue directory. Fix issue end. And I’m going to, within
this particular environment. So right now, we’re in the Qiskit Terra. We’re outside the Qiskit Terra folder. I’m going to go inside again and I’m going to say, “make style”. So this is the first
test that we’re running, and you’ll see what it’s going to do, is make sure that the code
is stylistically consistent. So we haven’t added maybe a space, instead of two spaces, that sort of thing. So no errors reported here. I’m also going to run, “make lint”. Lint is a much more thorough test so it’s going to consistently
look throughout the code using regular expressions and this can take a few moments. What you’re going to do is, once it reports whatever issues it finds, you’re going to look through and see if any of those
issues were caused by you or if they’re not caused by you. If they’re not, then
you’re ready to move on. Once the linting completes, you can see that there are no particular errors that I’ve caused. So the code has been rated 10 out of 10 and in particular, there’s this module
that reported one error but I wasn’t working in this folder so this must be something that
someone else is working on. So I’ll move on to the third
test and that’s simply, “make test” and this is a thorough code of testing the code itself. Once your tests finish, make sure that there are no errors of the particular files that you changed and if you see no errors, we’re ready to move on. So the next step now becomes, contributing this code back to GitHub. So I’m going to clear my terminal window. If you don’t know yet, the keyboard shortcut is command plus K. And what I’m going to do
then is say, “git status” to see where things are, and you see it tells you where
on this particular branch, we’ve changed this particular file and these things are untracked. They were probably created during the testing process that we ran. So in order to just submit the changes to these particular files and not these other ones. So I’ll do, “git add -u” and then I’ll say, “git status” again and now you see this particular code file that I changed is
green instead of red. All right, so this file is still on track. What I’m going to say is, “git commit -m” and I’m going to add a small piece of text to describe what I did so, fixed issue 2822 by updating, let’s see, by updating, matplotlib drawer. Okay! That’s it. And now again, let’s do, “git status”. You see here that the status shows you that there’s nothing that we’re
going to put in the commit. So now we’re ready to push
so we’ll say, “git push”. When you do that, you
get this error message because you’re working
on a specific branch so the fix is very simple, just copy this line over and run that. So, let’s copy this over. Okay, so now that that’s done, let’s go back to the GitHub page. So we’re going to go to GitHub.com/Qiskit and Qiskit Terra and you see this message that says, “Hey, you
just pushed something.” So let’s put a pull request
to put these changes forward and ask for a review. So I’m going to say,
“compare and pull request”. And now the text that I
wrote in the commit message comes in here. So fixed issue 2822 by updating. I’m going to say in the summary, “Fixes #2822”. So there’s a benefit to
doing something like this. So when you do this, so GitHub
automatically understands that you’re submitting this pull request to fix this issue so it goes and closes that issue once the pull request goes through. So let’s put in some comments. So I try to be as descriptive as I can in the details and comments and in the summary, I try to say, fixed the issue
number that we’re working on. Okay. So at this point, it’s a good
idea to look at the changes that you’re submitting. So here you can see Git is telling us that this used to be the old line of code and I’ve submitted this. So removed the parenthesis
and changed the limit to 28 characters. Okay, now we’re ready and there we go. So I’m going to create
a pull request here. At this point, now that you’ve
submitted the pull request, a few things are going to happen. First, some tests are
going to run automatically and you can see them
showing up on my screen. The other things that you can do, once the tests go through, is to sign a contributor license agreement if you haven’t signed it already. And that would happen here in this line. The final thing that will happen once all of these tests go through is that there are reviewers assigned to this particular repository. They’ll look through this and submit it and allow it to be part of
the code once it’s reasonable. So as you can see, we’ve gone through the entire process of fixing an issue in Qiskit Terra and what we did is
effectively find that issue in GitHub, download that
code to our computer, make those changes, run
tests to make sure that those changes don’t break anything, and submit those changes back to GitHub. And the changes that we’ve made here are quite impactful, right? So Qiskit is open-source software and this is one of the really cool parts about working in Qiskit. The changes that you’re making, the fixes that you’re
doing, are positive changes to a much bigger Quantum
computing community. And so there’s chance to make
a big impact impact here. So what I’d like to do is finish off by asking you to
contribute to Qiskit as well. So go to Qiskit Terra, find an issue that
looks interesting to you and tell us in the comments down below, what issue you’ve started contributing to and what pull requests you’ve submitted as a result of that work. We’ll see you in the comments! (upbeat pop music) (upbeat pop music) (upbeat pop music) (upbeat pop music

5 thoughts on “How To Contribute To Qiskit — Programming on Quantum Computers Ep 7”

Leave a Reply

Your email address will not be published. Required fields are marked *