-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
5 changed files
with
580 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.