Skip to content

Commit 24bac55

Browse files
authored
Typescript development in Cirq and prototype Bloch sphere implementation (#4171)
Typescript development in Cirq and prototype Bloch sphere implementation. ### Typescript Development in Cirq This PR implements the features discussed in the [Typescript Development in Cirq RFC](https://docs.google.com/document/d/16LybO9bNqL3LaIy1pZCJQXSPvKs5g3IqKkkE3efqM8U/edit). #### Listed below are the features included in the PR: - Infrastructure and directory structure to allow Typescript development in Cirq, including: - Bundling Typescript into Javascript, and examples of how to use these bundles - Hot reloading development environment for both browser and notebook settings - Typescript testing framework using Mocha and Chai, including a test for the bloch sphere. - Coverage checks for Typescript unit tests. (100% coverage needed in CI) - End-to-end integration between the Python and Javascript (the bloch sphere) - Example of full coverage testing of the Python side - CI for the Typescript environment for testing and linting. - Check scripts for testing and linting - An example 3d bloch sphere implementation. This bloch sphere can be viewed: - In the browser (development) - In a Jupyter notebook (development and production) - Via a generated HTML file. This file can be sent anywhere and be viewed in the browser, regardless of if the recipient has Cirq installed or not. - A [up-to-date package on Test PyPI](https://test.pypi.org/project/cirq-web/0.12.0.dev20210616161134/) representing the state of the cirq-web package as of 6/16/21. - Documentation for using cirq-web in the README files. ![colab_web_pr_trial](https://user-images.githubusercontent.com/17647506/122309538-1673ab80-ced4-11eb-96dd-112ea46b6df9.gif)
1 parent 6282c06 commit 24bac55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+14030
-6
lines changed

.github/workflows/ci.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,49 @@ jobs:
290290
with:
291291
name: notebook-outputs
292292
path: out
293+
ts-build:
294+
name: Bundle file consistency
295+
runs-on: ubuntu-20.04
296+
steps:
297+
- uses: actions/checkout@v2
298+
with:
299+
node-version: '14.16.1'
300+
- name: Install npm
301+
run: check/npm ci
302+
- name: Check build matches the current
303+
run: check/ts-build-current
304+
ts-lint:
305+
name: Typescript lint check
306+
runs-on: ubuntu-20.04
307+
steps:
308+
- uses: actions/checkout@v2
309+
with:
310+
node-version: '14.16.1'
311+
- name: Install npm
312+
run: check/npm ci
313+
- name: Lint
314+
run: check/ts-lint
315+
ts-test:
316+
name: Typescript tests
317+
runs-on: ubuntu-20.04
318+
steps:
319+
- uses: actions/checkout@v2
320+
with:
321+
node-version: '14.16.1'
322+
- name: Install npm dependencies
323+
run: check/npm ci
324+
- name: Unit tests
325+
run: check/ts-test
326+
- name: End to end tests
327+
run: check/ts-test-e2e
328+
ts-coverage:
329+
name: Typescript tests coverage
330+
runs-on: ubuntu-20.04
331+
steps:
332+
- uses: actions/checkout@v2
333+
with:
334+
node-version: '14.16.1'
335+
- name: Install npm dependencies
336+
run: check/npm ci
337+
- name: Test
338+
run: check/ts-coverage

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ docs/api_docs
4848
.python-version
4949

5050
# notebook test output
51-
out
51+
out

check/npm

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 The Cirq Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
################################################################################
18+
# Allows the user to run npm commands from the top-level Cirq
19+
# directory.
20+
#
21+
# Usage:
22+
# check/npm
23+
################################################################################
24+
25+
# Get the working directory to the repo root.
26+
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
27+
cd "$(git rev-parse --show-toplevel)"
28+
29+
npm --prefix 'cirq-web/cirq_ts' $@

check/npx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 The Cirq Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
################################################################################
18+
# Allows the user to run npx commands from the top-level directory
19+
#
20+
# Usage:
21+
# check/ts-build
22+
################################################################################
23+
24+
# Get the working directory to the repo root.
25+
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
26+
cd "$(git rev-parse --show-toplevel)"
27+
28+
cd 'cirq-web/cirq_ts'
29+
npx $@

check/ts-build

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 The Cirq Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
################################################################################
18+
# Build the bundle files for Cirq-web
19+
#
20+
# Usage:
21+
# check/ts-build
22+
################################################################################
23+
24+
# Get the working directory to the repo root.
25+
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
26+
cd "$(git rev-parse --show-toplevel)"
27+
28+
check/npx webpack --mode production $@

check/ts-build-current

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 The Cirq Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
################################################################################
18+
# Checks if all bundled files are up to date.
19+
#
20+
# Usage:
21+
# check/ts-build-changes
22+
################################################################################
23+
24+
check/ts-build
25+
26+
# Find the changed bundle.js files, if any
27+
untracked=$(git status --porcelain 2>/dev/null | grep "cirq-web/cirq_ts/dist/" | cut -d " " -f 3)
28+
29+
if [[ ! -z "$untracked" ]]; then
30+
echo -e "\033[31mERROR: Uncommitted changes to bundle file(s) found! Please commit these files:\033[0m"
31+
for generated in $untracked
32+
do
33+
echo -e "\033[31m ${generated}\033[0m"
34+
done
35+
exit 1
36+
fi
37+
38+
exit 0

check/ts-coverage

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 The Cirq Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
################################################################################
18+
# Runs the Istanbul coverage checker on the Typescript. Primarily used for CI.
19+
#
20+
# Usage:
21+
# check/ts-lint
22+
################################################################################
23+
24+
# Get the working directory to the repo root.
25+
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
26+
cd "$(git rev-parse --show-toplevel)"
27+
28+
check/npm run coverage $@

check/ts-lint

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 The Cirq Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
################################################################################
18+
# Runs the linter on the Typescript. Primarily used for CI.
19+
#
20+
# Usage:
21+
# check/ts-lint
22+
################################################################################
23+
24+
# Get the working directory to the repo root.
25+
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
26+
cd "$(git rev-parse --show-toplevel)"
27+
28+
check/npm run lint $@

check/ts-lint-and-format

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 The Cirq Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
################################################################################
18+
# Runs gts on the Typescript files in the cirq-web package
19+
#
20+
# Usage:
21+
# check/ts-lint-and-format
22+
################################################################################
23+
24+
# Get the working directory to the repo root.
25+
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
26+
cd "$(git rev-parse --show-toplevel)"
27+
28+
check/npm run fix $@

check/ts-test

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 The Cirq Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
################################################################################
18+
# Runs the test files
19+
#
20+
# Usage:
21+
# check/ts-test
22+
#
23+
# "npm run test" runs unit tests
24+
################################################################################
25+
26+
# Get the working directory to the repo root.
27+
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
28+
cd "$(git rev-parse --show-toplevel)"
29+
30+
check/npm run test $@

check/ts-test-e2e

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 The Cirq Developers
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
################################################################################
18+
# Runs the end to end tests and ensures that they're all passing
19+
#
20+
# Usage:
21+
# check/ts-test-e2e
22+
#
23+
# "npm run test-e2e" runs end to end tests
24+
################################################################################
25+
26+
# Get the working directory to the repo root.
27+
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
28+
cd "$(git rev-parse --show-toplevel)"
29+
30+
check/npm run test-e2e $@

cirq-web/.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Ignore node modules
2+
node_modules/
3+
4+
# Pytest files
5+
.pytest_cache/
6+
7+
# Coverage testing information
8+
.nyc_output/
9+
10+
# Extras
11+
build/
12+

0 commit comments

Comments
 (0)