Skip to content

Commit

Permalink
QCQMC Part 4 (#346)
Browse files Browse the repository at this point in the history
* Add shadow tomography module.

* Don't depend on trial_wf.

* Add test.

* Fix import.

* Incorporate docstring suggestions.

* Remove quaff test from CI.
  • Loading branch information
fdmalone authored Jun 19, 2024
1 parent 5656112 commit c81a9db
Show file tree
Hide file tree
Showing 5 changed files with 580 additions and 5 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ jobs:
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test third_party
run: |
pip install pytest
pytest third_party/quaff
- name: Test with pytest
run: |
pip install pytest
Expand Down
40 changes: 40 additions & 0 deletions recirq/qcqmc/bitstrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2024 Google
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import itertools
from typing import Iterable, Tuple


def get_bitstrings_a_b(*, n_orb: int, n_elec: int) -> Iterable[Tuple[bool, ...]]:
"""Iterates over bitstrings with the right symmetry assuming a_b ordering.
This function assumes that the first n_orb qubits correspond to the alpha
orbitals and the second n_orb qubits correspond to the beta orbitals. The
ordering within the alpha and beta sectors doesn't matter (because we
iterate over all bitstrings with Hamming weight n_elec//2 in each sector.
"""

if n_orb != n_elec:
raise NotImplementedError("n_orb must equal n_elec.")

initial_bitstring = tuple(False for _ in range(n_orb - n_elec // 2)) + tuple(
True for _ in range(n_elec // 2)
)

spin_sector_bitstrings = set()
for perm in itertools.permutations(initial_bitstring):
spin_sector_bitstrings.add(perm)

for bitstring_a, bitstring_b in itertools.product(spin_sector_bitstrings, repeat=2):
yield bitstring_a + bitstring_b
20 changes: 20 additions & 0 deletions recirq/qcqmc/bitstrings_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
import pytest
import scipy

from recirq.qcqmc.bitstrings import get_bitstrings_a_b


def test_get_bitstrings_a_b():
with pytest.raises(NotImplementedError):
list(get_bitstrings_a_b(n_orb=4, n_elec=3))

bitstrings = np.array(list(get_bitstrings_a_b(n_orb=4, n_elec=4)))

assert bitstrings.shape[0] == scipy.special.binom(4, 2) ** 2
assert bitstrings.shape[1] == 2 * 4 # n_qubits columns = 2 * n_orb.
hamming_weight_left = np.sum(bitstrings[:, 0:4], axis=1)
hamming_weight_right = np.sum(bitstrings[:, 4:8], axis=1)

assert np.all(hamming_weight_left == 2)
assert np.all(hamming_weight_right == 2)
Loading

0 comments on commit c81a9db

Please sign in to comment.