From cf6f7236d2560b57159747317384eebba60869c4 Mon Sep 17 00:00:00 2001 From: Valentin Senicourt Date: Fri, 24 Feb 2023 16:33:09 -0800 Subject: [PATCH 1/6] Depth function dramatically accelerated through breakthrough lookup dictionary technology. Needs to be cleaned up after input from team --- tangelo/linq/circuit.py | 44 +++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/tangelo/linq/circuit.py b/tangelo/linq/circuit.py index e0368960c..e8bb7eb4c 100644 --- a/tangelo/linq/circuit.py +++ b/tangelo/linq/circuit.py @@ -195,31 +195,45 @@ def check_index_valid(index): def depth(self): """ Return the depth of the quantum circuit, by computing the number of moments. Does not count - qubit initialization as a moment (unlike Cirq, for example). + qubit initialization as a moment (unlike Cirq, for example). Computes from scratch. """ + # List of qubit indices involved in each moment. Look up dict for latest moment for each index. moments = list() + latest_moment = dict() - for g in self._gates: + # Traverse gates and compute moments + for g in self: qubits = set(g.target) if g.control is None else set(g.target + g.control) if not moments: moments.append(qubits) + for i in qubits: + latest_moment[i] = 0 else: - # Find latest moment involving at least one of the qubits targeted by the current gate - # The current gate is part of the moment following that one - for i, m in reversed(list(enumerate(moments))): - if m & qubits: - if (i+1) < len(moments): - moments[i+1] = moments[i+1] | qubits - else: - moments.append(qubits) - break - # Case where none of the target qubits have been used before - elif i == 0: - moments[0] = moments[0] | qubits - + # Find latest moment involving one of the qubits targeted by the gate + # -1 means the qubit index was encountered for the very first time + b = max([latest_moment.get(i, -1) for i in qubits]) + for i in qubits: + latest_moment[i] = b + 1 + + # Case 1: Gate can be included in a previous moment + # Includes b = -1 case where all qubits are encountered for the 1st time + if (b + 1) < len(moments): + moments[b + 1] = moments[b + 1] | qubits + # Case 2: Gate is part of a new moment + else: + moments.append(qubits) return len(moments) + # Option 2: No moment info. Thats the whole thing. 2x speedup on h20_321g example + # latest_moment = dict() + # for g in self: + # qubits = set(g.target) if g.control is None else set(g.target + g.control) + # b = max([latest_moment.get(i, -1) for i in qubits]) + # for i in qubits: + # latest_moment[i] = b + 1 + # return max(latest_moment.values()) + 1 + def trim_qubits(self): """Trim unnecessary qubits and update indices with the lowest values possible. """ From d77988d6d73599ef95b646e9693a3e39bdd611fa Mon Sep 17 00:00:00 2001 From: Valentin Senicourt Date: Mon, 27 Feb 2023 10:39:40 -0800 Subject: [PATCH 2/6] Clean up --- tangelo/linq/circuit.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/tangelo/linq/circuit.py b/tangelo/linq/circuit.py index e8bb7eb4c..730521d77 100644 --- a/tangelo/linq/circuit.py +++ b/tangelo/linq/circuit.py @@ -195,7 +195,7 @@ def check_index_valid(index): def depth(self): """ Return the depth of the quantum circuit, by computing the number of moments. Does not count - qubit initialization as a moment (unlike Cirq, for example). Computes from scratch. + qubit initialization as a moment (unlike Cirq, for example). Compute from scratch. """ # List of qubit indices involved in each moment. Look up dict for latest moment for each index. moments = list() @@ -225,15 +225,6 @@ def depth(self): moments.append(qubits) return len(moments) - # Option 2: No moment info. Thats the whole thing. 2x speedup on h20_321g example - # latest_moment = dict() - # for g in self: - # qubits = set(g.target) if g.control is None else set(g.target + g.control) - # b = max([latest_moment.get(i, -1) for i in qubits]) - # for i in qubits: - # latest_moment[i] = b + 1 - # return max(latest_moment.values()) + 1 - def trim_qubits(self): """Trim unnecessary qubits and update indices with the lowest values possible. """ From 3ccbb03d5985dd866149b2fb9b821ab2ab8033e7 Mon Sep 17 00:00:00 2001 From: Valentin Senicourt Date: Wed, 17 May 2023 22:43:06 +0200 Subject: [PATCH 3/6] Update to CONTRIB (main -> develop) and added workflow to help avoid PR to main that are not from develop --- .github/workflows/protect_main.yaml | 14 ++++++++++++++ CONTRIBUTIONS.rst | 13 +++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/protect_main.yaml diff --git a/.github/workflows/protect_main.yaml b/.github/workflows/protect_main.yaml new file mode 100644 index 000000000..7eaef2a64 --- /dev/null +++ b/.github/workflows/protect_main.yaml @@ -0,0 +1,14 @@ +name: 'Protect Main Branch' + +on: + pull_request: + +jobs: + check_branch: + runs-on: ubuntu-latest + steps: + - name: Check branch + if: github.base_ref == 'main' && github.head_ref != 'develop' + run: | + echo "ERROR: You can only merge to main from develop." + exit 1 diff --git a/CONTRIBUTIONS.rst b/CONTRIBUTIONS.rst index 0133184b1..5155d731f 100644 --- a/CONTRIBUTIONS.rst +++ b/CONTRIBUTIONS.rst @@ -43,24 +43,25 @@ In your terminal, clone the repo on your local machine, and move into the newly cd Tangelo From the perspective of your local clone, your fork is called the ``origin`` remote. -Let's synchronize your fork with the main Tangelo repo by adding the latter as the upstream remote, and then update your local ``main`` branch: +Let's synchronize your fork with the main Tangelo repo by adding the latter as the upstream remote, and then update your local ``develop`` branch: .. code-block:: shell git remote add upstream https://github.com/goodchemistryco/Tangelo.git git fetch upstream - git checkout main - git merge upstream/main + git checkout develop + git merge upstream/develop +Note: we here suggest the ``develop`` branch, as this is where contributions will be merged. No one should be merging directly to ``main``, unless it is to sync it with ``develop`` once in a while, and just before a new version release. **2. Work on your own developments** -Create your development branch, based on the ``main`` branch (or the current development branch listed on the `DevBranch badge <./README.rst>`_) +Create your development branch, based on the ``develop`` branch (or the current development branch listed on the `DevBranch badge <./README.rst>`_, if different) .. code-block:: shell - git checkout main -b your_branch_name + git checkout develop -b your_branch_name where ``your_branch_name`` is the name of your own development branch, preferably related to what you will be working on. Let's assume you've made some changes and committed them with ``git commit``, and that you'd like to push them to your fork (which is referred to as "origin"): @@ -72,7 +73,7 @@ Let's assume you've made some changes and committed them with ``git commit``, an **3. The Pull Request (PR)** -Now when you go to https://github.com/goodchemistryco/Tangelo, you should be able to create a pull request from the branch on your fork to a branch on the main Tangelo repo. Give your pull request a name and briefly describe what the purpose is and include a reference to the associated issue if there's one. +Now when you go to https://github.com/goodchemistryco/Tangelo, you should be able to create a pull request from the branch on your fork to a branch on the main Tangelo repo. Give your pull request a name, verify that the destination branch is ``develop`` (not ``main``), and briefly describe what the purpose is / include a reference to the associated issue if there's one. Several Tangelo users will receive a notification, and will review your code and leave comments in the PR. You can reply to these comments, or simply apply the recommended changes locally, and then commit and push them like above: it automatically updates your PR. If there are conflicts, you can solve them locally and push, or directly through Github. From 05bd6f60f971ed105fbb5c80f2cfbf49c7d772e6 Mon Sep 17 00:00:00 2001 From: Valentin Senicourt Date: Wed, 17 May 2023 22:57:43 +0200 Subject: [PATCH 4/6] Tweak workflow for debug --- .github/workflows/protect_main.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/protect_main.yaml b/.github/workflows/protect_main.yaml index 7eaef2a64..3f338f4e1 100644 --- a/.github/workflows/protect_main.yaml +++ b/.github/workflows/protect_main.yaml @@ -7,6 +7,9 @@ jobs: check_branch: runs-on: ubuntu-latest steps: + - name: print branches [debug] + if: always() + run: echo "Base ref branch ={github.base_ref}, Head ref branch = {github.head_ref}" - name: Check branch if: github.base_ref == 'main' && github.head_ref != 'develop' run: | From 1bd12bec4df0fe63e8c62435b38957ec91a41ca8 Mon Sep 17 00:00:00 2001 From: Valentin Senicourt Date: Wed, 17 May 2023 23:00:04 +0200 Subject: [PATCH 5/6] Tweak workflow for debug --- .github/workflows/protect_main.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/protect_main.yaml b/.github/workflows/protect_main.yaml index 3f338f4e1..7eaef2a64 100644 --- a/.github/workflows/protect_main.yaml +++ b/.github/workflows/protect_main.yaml @@ -7,9 +7,6 @@ jobs: check_branch: runs-on: ubuntu-latest steps: - - name: print branches [debug] - if: always() - run: echo "Base ref branch ={github.base_ref}, Head ref branch = {github.head_ref}" - name: Check branch if: github.base_ref == 'main' && github.head_ref != 'develop' run: | From 86a5f9841b854555c97f4250bee9d82a30b73a74 Mon Sep 17 00:00:00 2001 From: Valentin Senicourt Date: Wed, 17 May 2023 23:06:26 +0200 Subject: [PATCH 6/6] Tweak workflow for debug --- .github/workflows/protect_main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/protect_main.yaml b/.github/workflows/protect_main.yaml index 7eaef2a64..4e095b09f 100644 --- a/.github/workflows/protect_main.yaml +++ b/.github/workflows/protect_main.yaml @@ -10,5 +10,5 @@ jobs: - name: Check branch if: github.base_ref == 'main' && github.head_ref != 'develop' run: | - echo "ERROR: You can only merge to main from develop." + echo "ERROR: You can only merge to main from develop. Make sure your PR merges into the right branch." exit 1