From 8d7f5b6e2702b9588f45ef678701e84968997b6a Mon Sep 17 00:00:00 2001 From: Anna Nachesa Date: Wed, 27 Oct 2021 20:40:36 +0200 Subject: [PATCH] Moved GateTabulation and friends to cirq-core as requested in #4461 --- cirq-core/cirq/__init__.py | 2 ++ cirq-core/cirq/json_resolver_cache.py | 1 + cirq-core/cirq/optimizers/__init__.py | 6 ++++++ .../optimizers/two_qubit_gate_compilation.py | 7 +++---- .../two_qubit_gate_compilation_test.py | 8 +++----- .../cirq/optimizers/two_qubit_gate_math_utils.py | 0 .../optimizers/two_qubit_gate_math_utils_test.py | 2 +- .../json_test_data/GateTabulation.json | 0 .../json_test_data/GateTabulation.json_inward | 0 .../protocols/json_test_data/GateTabulation.repr | 1 + .../json_test_data/GateTabulation.repr_inward | 1 + cirq-google/cirq_google/__init__.py | 2 +- cirq-google/cirq_google/json_resolver_cache.py | 1 - .../json_test_data/GateTabulation.repr | 1 - .../json_test_data/GateTabulation.repr_inward | 1 - cirq-google/cirq_google/optimizers/__init__.py | 8 ++++---- .../optimizers/convert_to_sycamore_gates.py | 13 +++++++------ .../optimizers/convert_to_sycamore_gates_test.py | 3 +-- .../optimizers/optimize_for_sycamore.py | 16 +++++++--------- .../optimizers/two_qubit_gates/__init__.py | 4 ---- .../optimizers/two_qubit_gates/example.py | 5 ++--- 21 files changed, 40 insertions(+), 42 deletions(-) rename cirq-google/cirq_google/optimizers/two_qubit_gates/gate_compilation.py => cirq-core/cirq/optimizers/two_qubit_gate_compilation.py (98%) rename cirq-google/cirq_google/optimizers/two_qubit_gates/gate_compilation_test.py => cirq-core/cirq/optimizers/two_qubit_gate_compilation_test.py (91%) rename cirq-google/cirq_google/optimizers/two_qubit_gates/math_utils.py => cirq-core/cirq/optimizers/two_qubit_gate_math_utils.py (100%) rename cirq-google/cirq_google/optimizers/two_qubit_gates/math_utils_test.py => cirq-core/cirq/optimizers/two_qubit_gate_math_utils_test.py (95%) rename {cirq-google/cirq_google => cirq-core/cirq/protocols}/json_test_data/GateTabulation.json (100%) rename {cirq-google/cirq_google => cirq-core/cirq/protocols}/json_test_data/GateTabulation.json_inward (100%) create mode 100644 cirq-core/cirq/protocols/json_test_data/GateTabulation.repr create mode 100644 cirq-core/cirq/protocols/json_test_data/GateTabulation.repr_inward delete mode 100644 cirq-google/cirq_google/json_test_data/GateTabulation.repr delete mode 100644 cirq-google/cirq_google/json_test_data/GateTabulation.repr_inward delete mode 100644 cirq-google/cirq_google/optimizers/two_qubit_gates/__init__.py diff --git a/cirq-core/cirq/__init__.py b/cirq-core/cirq/__init__.py index 8128989923d..30360478559 100644 --- a/cirq-core/cirq/__init__.py +++ b/cirq-core/cirq/__init__.py @@ -339,6 +339,8 @@ EjectPhasedPaulis, EjectZ, ExpandComposite, + gate_product_tabulation, + GateTabulation, is_negligible_turn, merge_single_qubit_gates_into_phased_x_z, merge_single_qubit_gates_into_phxz, diff --git a/cirq-core/cirq/json_resolver_cache.py b/cirq-core/cirq/json_resolver_cache.py index 22fc37a0319..67882d44c26 100644 --- a/cirq-core/cirq/json_resolver_cache.py +++ b/cirq-core/cirq/json_resolver_cache.py @@ -79,6 +79,7 @@ def two_qubit_matrix_gate(matrix): 'MutablePauliString': cirq.MutablePauliString, 'ObservableMeasuredResult': cirq.work.ObservableMeasuredResult, 'GateOperation': cirq.GateOperation, + 'GateTabulation': cirq.GateTabulation, 'GeneralizedAmplitudeDampingChannel': cirq.GeneralizedAmplitudeDampingChannel, 'GlobalPhaseOperation': cirq.GlobalPhaseOperation, 'GridInteractionLayer': GridInteractionLayer, diff --git a/cirq-core/cirq/optimizers/__init__.py b/cirq-core/cirq/optimizers/__init__.py index 9a1ab286dca..009d7a879fb 100644 --- a/cirq-core/cirq/optimizers/__init__.py +++ b/cirq-core/cirq/optimizers/__init__.py @@ -96,6 +96,12 @@ two_qubit_matrix_to_operations, two_qubit_matrix_to_diagonal_and_operations, ) + +from cirq.optimizers.two_qubit_gate_compilation import ( + gate_product_tabulation, + GateTabulation, +) + from cirq.optimizers.two_qubit_to_sqrt_iswap import ( two_qubit_matrix_to_sqrt_iswap_operations, ) diff --git a/cirq-google/cirq_google/optimizers/two_qubit_gates/gate_compilation.py b/cirq-core/cirq/optimizers/two_qubit_gate_compilation.py similarity index 98% rename from cirq-google/cirq_google/optimizers/two_qubit_gates/gate_compilation.py rename to cirq-core/cirq/optimizers/two_qubit_gate_compilation.py index d291f86090f..3c2533238e6 100644 --- a/cirq-google/cirq_google/optimizers/two_qubit_gates/gate_compilation.py +++ b/cirq-core/cirq/optimizers/two_qubit_gate_compilation.py @@ -9,7 +9,7 @@ import cirq from cirq import value from cirq._compat import proper_repr, proper_eq -from cirq_google.optimizers.two_qubit_gates.math_utils import ( +from cirq.optimizers.two_qubit_gate_math_utils import ( kak_vector_infidelity, vector_kron, weyl_chamber_mesh, @@ -133,8 +133,7 @@ def __repr__(self) -> str: numpy_single_qubit_gates.append(f"[{','.join(gate_repr)}]") return ( - f'cirq_google.optimizers.two_qubit_gates.gate_compilation' - f'.GateTabulation({proper_repr(self.base_gate)}, ' + f'cirq.GateTabulation({proper_repr(self.base_gate)}, ' f'{proper_repr(self.kak_vecs)}, ' f'[{",".join(numpy_single_qubit_gates)}], ' f' {proper_repr(self.max_expected_infidelity)}, ' @@ -305,7 +304,7 @@ def gate_product_tabulation( *, sample_scaling: int = 50, allow_missed_points: bool = True, - random_state: cirq.RANDOM_STATE_OR_SEED_LIKE = None, + random_state: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, ) -> GateTabulation: r"""Generate a GateTabulation for a base two qubit unitary. diff --git a/cirq-google/cirq_google/optimizers/two_qubit_gates/gate_compilation_test.py b/cirq-core/cirq/optimizers/two_qubit_gate_compilation_test.py similarity index 91% rename from cirq-google/cirq_google/optimizers/two_qubit_gates/gate_compilation_test.py rename to cirq-core/cirq/optimizers/two_qubit_gate_compilation_test.py index da5e59fb7ef..146f7accc7b 100644 --- a/cirq-google/cirq_google/optimizers/two_qubit_gates/gate_compilation_test.py +++ b/cirq-core/cirq/optimizers/two_qubit_gate_compilation_test.py @@ -4,11 +4,11 @@ import cirq from cirq import value -from cirq_google.optimizers.two_qubit_gates.gate_compilation import ( +from cirq.optimizers.two_qubit_gate_compilation import ( gate_product_tabulation, GateTabulation, ) -from cirq_google.optimizers.two_qubit_gates.math_utils import unitary_entanglement_fidelity +from cirq.optimizers.two_qubit_gate_math_utils import unitary_entanglement_fidelity from cirq.testing import random_special_unitary, assert_equivalent_repr _rng = value.parse_random_state(11) # for determinism @@ -82,9 +82,7 @@ def test_sycamore_gate_tabulation_repr(): 'Sample string', (), ) - assert_equivalent_repr( - simple_tabulation, setup_code="import cirq\nimport cirq_google\nimport numpy as np" - ) + assert_equivalent_repr(simple_tabulation) def test_sycamore_gate_tabulation_eq(): diff --git a/cirq-google/cirq_google/optimizers/two_qubit_gates/math_utils.py b/cirq-core/cirq/optimizers/two_qubit_gate_math_utils.py similarity index 100% rename from cirq-google/cirq_google/optimizers/two_qubit_gates/math_utils.py rename to cirq-core/cirq/optimizers/two_qubit_gate_math_utils.py diff --git a/cirq-google/cirq_google/optimizers/two_qubit_gates/math_utils_test.py b/cirq-core/cirq/optimizers/two_qubit_gate_math_utils_test.py similarity index 95% rename from cirq-google/cirq_google/optimizers/two_qubit_gates/math_utils_test.py rename to cirq-core/cirq/optimizers/two_qubit_gate_math_utils_test.py index 06fba00a4a4..ed42634fdb2 100644 --- a/cirq-google/cirq_google/optimizers/two_qubit_gates/math_utils_test.py +++ b/cirq-core/cirq/optimizers/two_qubit_gate_math_utils_test.py @@ -3,7 +3,7 @@ import cirq from cirq import value -from cirq_google.optimizers.two_qubit_gates.math_utils import ( +from cirq.optimizers.two_qubit_gate_math_utils import ( weyl_chamber_mesh, kak_vector_infidelity, random_qubit_unitary, diff --git a/cirq-google/cirq_google/json_test_data/GateTabulation.json b/cirq-core/cirq/protocols/json_test_data/GateTabulation.json similarity index 100% rename from cirq-google/cirq_google/json_test_data/GateTabulation.json rename to cirq-core/cirq/protocols/json_test_data/GateTabulation.json diff --git a/cirq-google/cirq_google/json_test_data/GateTabulation.json_inward b/cirq-core/cirq/protocols/json_test_data/GateTabulation.json_inward similarity index 100% rename from cirq-google/cirq_google/json_test_data/GateTabulation.json_inward rename to cirq-core/cirq/protocols/json_test_data/GateTabulation.json_inward diff --git a/cirq-core/cirq/protocols/json_test_data/GateTabulation.repr b/cirq-core/cirq/protocols/json_test_data/GateTabulation.repr new file mode 100644 index 00000000000..dedc3434863 --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/GateTabulation.repr @@ -0,0 +1 @@ +cirq.GateTabulation(np.array([[(1+0j), 0j, 0j, 0j], [0j, (6.123233995736766e-17+0j), -1j, 0j], [0j, -1j, (6.123233995736766e-17+0j), 0j], [0j, 0j, 0j, (0.8660254037844387-0.49999999999999994j)]], dtype=np.complex128), np.array([[0.7853981633974483, 0.7853981633974483, 0.1308996938995748], [0.5455546451806152, 0.4274250500660468, 1.1102230246251565e-16], [0.482263980185593, 0.15541244226118112, -0.0], [0.08994436657892213, 0.0688663429298958, -0.0], [0.5337019863142523, 0.31800967733278185, -0.2565859360625904], [0.48438278888761155, 0.39121774851710633, 0.28728266960007875]], dtype=np.float64), [[],[(np.array([[(-0.8113361037323902+0.388228151720065j), (0.38855300559639544-0.20009795309891054j)], [(-0.38855300559639544-0.20009795309891054j), (-0.8113361037323902-0.388228151720065j)]], dtype=np.complex128), np.array([[(-0.15500592487333173-0.5209557609876208j), (-0.6259723589563565+0.5592288119996914j)], [(0.6259723589563565+0.5592288119996914j), (-0.15500592487333173+0.5209557609876208j)]], dtype=np.complex128))],[(np.array([[(-0.7591335658137299-0.5262321684934362j), (0.37132129473675946+0.09442685090928131j)], [(-0.37132129473675946+0.09442685090928131j), (-0.7591335658137299+0.5262321684934362j)]], dtype=np.complex128), np.array([[(0.004722472920167423+0.9809944588837683j), (0.18002103957975657+0.07224953423714474j)], [(-0.18002103957975657+0.07224953423714474j), (0.004722472920167423-0.9809944588837683j)]], dtype=np.complex128))],[(np.array([[(-0.0897362442086916+0.02445105905520244j), (-0.8503296766825128-0.5179662084918382j)], [(0.8503296766825128-0.5179662084918382j), (-0.0897362442086916-0.02445105905520244j)]], dtype=np.complex128), np.array([[(0.7441127742234152+0.6642425418602642j), (0.009545063892332065+0.07061810374004159j)], [(-0.009545063892332065+0.07061810374004159j), (0.7441127742234152-0.6642425418602642j)]], dtype=np.complex128))],[(np.array([[(-0.8113361037323902+0.388228151720065j), (0.38855300559639544-0.20009795309891054j)], [(-0.38855300559639544-0.20009795309891054j), (-0.8113361037323902-0.388228151720065j)]], dtype=np.complex128), np.array([[(-0.15500592487333173-0.5209557609876208j), (-0.6259723589563565+0.5592288119996914j)], [(0.6259723589563565+0.5592288119996914j), (-0.15500592487333173+0.5209557609876208j)]], dtype=np.complex128)),(np.array([[(-0.8113361037323902+0.388228151720065j), (0.38855300559639544-0.20009795309891054j)], [(-0.38855300559639544-0.20009795309891054j), (-0.8113361037323902-0.388228151720065j)]], dtype=np.complex128), np.array([[(-0.15500592487333173-0.5209557609876208j), (-0.6259723589563565+0.5592288119996914j)], [(0.6259723589563565+0.5592288119996914j), (-0.15500592487333173+0.5209557609876208j)]], dtype=np.complex128))],[(np.array([[(0.41295648139511504-0.17280776715854063j), (0.5430881450957462-0.7103940362502399j)], [(-0.5430881450957462-0.7103940362502399j), (0.41295648139511504+0.17280776715854063j)]], dtype=np.complex128), np.array([[(0.19013479709876246+0.4941656851052965j), (-0.6469746262664201+0.5487010730480224j)], [(0.6469746262664201+0.5487010730480224j), (0.19013479709876246-0.4941656851052965j)]], dtype=np.complex128)),(np.array([[(0.41295648139511504-0.17280776715854063j), (0.5430881450957462-0.7103940362502399j)], [(-0.5430881450957462-0.7103940362502399j), (0.41295648139511504+0.17280776715854063j)]], dtype=np.complex128), np.array([[(0.19013479709876246+0.4941656851052965j), (-0.6469746262664201+0.5487010730480224j)], [(0.6469746262664201+0.5487010730480224j), (0.19013479709876246-0.4941656851052965j)]], dtype=np.complex128))]], 0.49, 'Fraction of Weyl chamber reached with 2 gates: 0.600\nFraction of Weyl chamber reached with 2 gates and 3 gates(same single qubit): 1.000', ()) \ No newline at end of file diff --git a/cirq-core/cirq/protocols/json_test_data/GateTabulation.repr_inward b/cirq-core/cirq/protocols/json_test_data/GateTabulation.repr_inward new file mode 100644 index 00000000000..dedc3434863 --- /dev/null +++ b/cirq-core/cirq/protocols/json_test_data/GateTabulation.repr_inward @@ -0,0 +1 @@ +cirq.GateTabulation(np.array([[(1+0j), 0j, 0j, 0j], [0j, (6.123233995736766e-17+0j), -1j, 0j], [0j, -1j, (6.123233995736766e-17+0j), 0j], [0j, 0j, 0j, (0.8660254037844387-0.49999999999999994j)]], dtype=np.complex128), np.array([[0.7853981633974483, 0.7853981633974483, 0.1308996938995748], [0.5455546451806152, 0.4274250500660468, 1.1102230246251565e-16], [0.482263980185593, 0.15541244226118112, -0.0], [0.08994436657892213, 0.0688663429298958, -0.0], [0.5337019863142523, 0.31800967733278185, -0.2565859360625904], [0.48438278888761155, 0.39121774851710633, 0.28728266960007875]], dtype=np.float64), [[],[(np.array([[(-0.8113361037323902+0.388228151720065j), (0.38855300559639544-0.20009795309891054j)], [(-0.38855300559639544-0.20009795309891054j), (-0.8113361037323902-0.388228151720065j)]], dtype=np.complex128), np.array([[(-0.15500592487333173-0.5209557609876208j), (-0.6259723589563565+0.5592288119996914j)], [(0.6259723589563565+0.5592288119996914j), (-0.15500592487333173+0.5209557609876208j)]], dtype=np.complex128))],[(np.array([[(-0.7591335658137299-0.5262321684934362j), (0.37132129473675946+0.09442685090928131j)], [(-0.37132129473675946+0.09442685090928131j), (-0.7591335658137299+0.5262321684934362j)]], dtype=np.complex128), np.array([[(0.004722472920167423+0.9809944588837683j), (0.18002103957975657+0.07224953423714474j)], [(-0.18002103957975657+0.07224953423714474j), (0.004722472920167423-0.9809944588837683j)]], dtype=np.complex128))],[(np.array([[(-0.0897362442086916+0.02445105905520244j), (-0.8503296766825128-0.5179662084918382j)], [(0.8503296766825128-0.5179662084918382j), (-0.0897362442086916-0.02445105905520244j)]], dtype=np.complex128), np.array([[(0.7441127742234152+0.6642425418602642j), (0.009545063892332065+0.07061810374004159j)], [(-0.009545063892332065+0.07061810374004159j), (0.7441127742234152-0.6642425418602642j)]], dtype=np.complex128))],[(np.array([[(-0.8113361037323902+0.388228151720065j), (0.38855300559639544-0.20009795309891054j)], [(-0.38855300559639544-0.20009795309891054j), (-0.8113361037323902-0.388228151720065j)]], dtype=np.complex128), np.array([[(-0.15500592487333173-0.5209557609876208j), (-0.6259723589563565+0.5592288119996914j)], [(0.6259723589563565+0.5592288119996914j), (-0.15500592487333173+0.5209557609876208j)]], dtype=np.complex128)),(np.array([[(-0.8113361037323902+0.388228151720065j), (0.38855300559639544-0.20009795309891054j)], [(-0.38855300559639544-0.20009795309891054j), (-0.8113361037323902-0.388228151720065j)]], dtype=np.complex128), np.array([[(-0.15500592487333173-0.5209557609876208j), (-0.6259723589563565+0.5592288119996914j)], [(0.6259723589563565+0.5592288119996914j), (-0.15500592487333173+0.5209557609876208j)]], dtype=np.complex128))],[(np.array([[(0.41295648139511504-0.17280776715854063j), (0.5430881450957462-0.7103940362502399j)], [(-0.5430881450957462-0.7103940362502399j), (0.41295648139511504+0.17280776715854063j)]], dtype=np.complex128), np.array([[(0.19013479709876246+0.4941656851052965j), (-0.6469746262664201+0.5487010730480224j)], [(0.6469746262664201+0.5487010730480224j), (0.19013479709876246-0.4941656851052965j)]], dtype=np.complex128)),(np.array([[(0.41295648139511504-0.17280776715854063j), (0.5430881450957462-0.7103940362502399j)], [(-0.5430881450957462-0.7103940362502399j), (0.41295648139511504+0.17280776715854063j)]], dtype=np.complex128), np.array([[(0.19013479709876246+0.4941656851052965j), (-0.6469746262664201+0.5487010730480224j)], [(0.6469746262664201+0.5487010730480224j), (0.19013479709876246-0.4941656851052965j)]], dtype=np.complex128))]], 0.49, 'Fraction of Weyl chamber reached with 2 gates: 0.600\nFraction of Weyl chamber reached with 2 gates and 3 gates(same single qubit): 1.000', ()) \ No newline at end of file diff --git a/cirq-google/cirq_google/__init__.py b/cirq-google/cirq_google/__init__.py index 4c66402529a..95ff3bfb2c1 100644 --- a/cirq-google/cirq_google/__init__.py +++ b/cirq-google/cirq_google/__init__.py @@ -94,7 +94,7 @@ ConvertToXmonGates, ConvertToSqrtIswapGates, ConvertToSycamoreGates, - GateTabulation, + # GateTabulation, optimized_for_sycamore, optimized_for_xmon, ) diff --git a/cirq-google/cirq_google/json_resolver_cache.py b/cirq-google/cirq_google/json_resolver_cache.py index 4f165d750bb..25a257b4279 100644 --- a/cirq-google/cirq_google/json_resolver_cache.py +++ b/cirq-google/cirq_google/json_resolver_cache.py @@ -31,7 +31,6 @@ def _class_resolver_dictionary() -> Dict[str, ObjectFactory]: 'CalibrationResult': cirq_google.CalibrationResult, 'CouplerPulse': cirq_google.experimental.CouplerPulse, 'SycamoreGate': cirq_google.SycamoreGate, - 'GateTabulation': cirq_google.GateTabulation, 'PhysicalZTag': cirq_google.PhysicalZTag, 'FloquetPhasedFSimCalibrationOptions': cirq_google.FloquetPhasedFSimCalibrationOptions, 'FloquetPhasedFSimCalibrationRequest': cirq_google.FloquetPhasedFSimCalibrationRequest, diff --git a/cirq-google/cirq_google/json_test_data/GateTabulation.repr b/cirq-google/cirq_google/json_test_data/GateTabulation.repr deleted file mode 100644 index 91e51fb075c..00000000000 --- a/cirq-google/cirq_google/json_test_data/GateTabulation.repr +++ /dev/null @@ -1 +0,0 @@ -cirq_google.optimizers.two_qubit_gates.gate_compilation.GateTabulation(np.array([[(1+0j), 0j, 0j, 0j], [0j, (6.123233995736766e-17+0j), -1j, 0j], [0j, -1j, (6.123233995736766e-17+0j), 0j], [0j, 0j, 0j, (0.8660254037844387-0.49999999999999994j)]], dtype=np.complex128), np.array([[0.7853981633974483, 0.7853981633974483, 0.1308996938995748], [0.5455546451806152, 0.4274250500660468, 1.1102230246251565e-16], [0.482263980185593, 0.15541244226118112, -0.0], [0.08994436657892213, 0.0688663429298958, -0.0], [0.5337019863142523, 0.31800967733278185, -0.2565859360625904], [0.48438278888761155, 0.39121774851710633, 0.28728266960007875]], dtype=np.float64), [[],[(np.array([[(-0.8113361037323902+0.388228151720065j), (0.38855300559639544-0.20009795309891054j)], [(-0.38855300559639544-0.20009795309891054j), (-0.8113361037323902-0.388228151720065j)]], dtype=np.complex128), np.array([[(-0.15500592487333173-0.5209557609876208j), (-0.6259723589563565+0.5592288119996914j)], [(0.6259723589563565+0.5592288119996914j), (-0.15500592487333173+0.5209557609876208j)]], dtype=np.complex128))],[(np.array([[(-0.7591335658137299-0.5262321684934362j), (0.37132129473675946+0.09442685090928131j)], [(-0.37132129473675946+0.09442685090928131j), (-0.7591335658137299+0.5262321684934362j)]], dtype=np.complex128), np.array([[(0.004722472920167423+0.9809944588837683j), (0.18002103957975657+0.07224953423714474j)], [(-0.18002103957975657+0.07224953423714474j), (0.004722472920167423-0.9809944588837683j)]], dtype=np.complex128))],[(np.array([[(-0.0897362442086916+0.02445105905520244j), (-0.8503296766825128-0.5179662084918382j)], [(0.8503296766825128-0.5179662084918382j), (-0.0897362442086916-0.02445105905520244j)]], dtype=np.complex128), np.array([[(0.7441127742234152+0.6642425418602642j), (0.009545063892332065+0.07061810374004159j)], [(-0.009545063892332065+0.07061810374004159j), (0.7441127742234152-0.6642425418602642j)]], dtype=np.complex128))],[(np.array([[(-0.8113361037323902+0.388228151720065j), (0.38855300559639544-0.20009795309891054j)], [(-0.38855300559639544-0.20009795309891054j), (-0.8113361037323902-0.388228151720065j)]], dtype=np.complex128), np.array([[(-0.15500592487333173-0.5209557609876208j), (-0.6259723589563565+0.5592288119996914j)], [(0.6259723589563565+0.5592288119996914j), (-0.15500592487333173+0.5209557609876208j)]], dtype=np.complex128)),(np.array([[(-0.8113361037323902+0.388228151720065j), (0.38855300559639544-0.20009795309891054j)], [(-0.38855300559639544-0.20009795309891054j), (-0.8113361037323902-0.388228151720065j)]], dtype=np.complex128), np.array([[(-0.15500592487333173-0.5209557609876208j), (-0.6259723589563565+0.5592288119996914j)], [(0.6259723589563565+0.5592288119996914j), (-0.15500592487333173+0.5209557609876208j)]], dtype=np.complex128))],[(np.array([[(0.41295648139511504-0.17280776715854063j), (0.5430881450957462-0.7103940362502399j)], [(-0.5430881450957462-0.7103940362502399j), (0.41295648139511504+0.17280776715854063j)]], dtype=np.complex128), np.array([[(0.19013479709876246+0.4941656851052965j), (-0.6469746262664201+0.5487010730480224j)], [(0.6469746262664201+0.5487010730480224j), (0.19013479709876246-0.4941656851052965j)]], dtype=np.complex128)),(np.array([[(0.41295648139511504-0.17280776715854063j), (0.5430881450957462-0.7103940362502399j)], [(-0.5430881450957462-0.7103940362502399j), (0.41295648139511504+0.17280776715854063j)]], dtype=np.complex128), np.array([[(0.19013479709876246+0.4941656851052965j), (-0.6469746262664201+0.5487010730480224j)], [(0.6469746262664201+0.5487010730480224j), (0.19013479709876246-0.4941656851052965j)]], dtype=np.complex128))]], 0.49, 'Fraction of Weyl chamber reached with 2 gates: 0.600\nFraction of Weyl chamber reached with 2 gates and 3 gates(same single qubit): 1.000', ()) \ No newline at end of file diff --git a/cirq-google/cirq_google/json_test_data/GateTabulation.repr_inward b/cirq-google/cirq_google/json_test_data/GateTabulation.repr_inward deleted file mode 100644 index 45a01d2bfd6..00000000000 --- a/cirq-google/cirq_google/json_test_data/GateTabulation.repr_inward +++ /dev/null @@ -1 +0,0 @@ -cirq.google.optimizers.two_qubit_gates.gate_compilation.GateTabulation(np.array([[(1+0j), 0j, 0j, 0j], [0j, (6.123233995736766e-17+0j), -1j, 0j], [0j, -1j, (6.123233995736766e-17+0j), 0j], [0j, 0j, 0j, (0.8660254037844387-0.49999999999999994j)]], dtype=np.complex128), np.array([[0.7853981633974483, 0.7853981633974483, 0.1308996938995748], [0.5455546451806152, 0.4274250500660468, 1.1102230246251565e-16], [0.482263980185593, 0.15541244226118112, -0.0], [0.08994436657892213, 0.0688663429298958, -0.0], [0.5337019863142523, 0.31800967733278185, -0.2565859360625904], [0.48438278888761155, 0.39121774851710633, 0.28728266960007875]], dtype=np.float64), [[],[(np.array([[(-0.8113361037323902+0.388228151720065j), (0.38855300559639544-0.20009795309891054j)], [(-0.38855300559639544-0.20009795309891054j), (-0.8113361037323902-0.388228151720065j)]], dtype=np.complex128), np.array([[(-0.15500592487333173-0.5209557609876208j), (-0.6259723589563565+0.5592288119996914j)], [(0.6259723589563565+0.5592288119996914j), (-0.15500592487333173+0.5209557609876208j)]], dtype=np.complex128))],[(np.array([[(-0.7591335658137299-0.5262321684934362j), (0.37132129473675946+0.09442685090928131j)], [(-0.37132129473675946+0.09442685090928131j), (-0.7591335658137299+0.5262321684934362j)]], dtype=np.complex128), np.array([[(0.004722472920167423+0.9809944588837683j), (0.18002103957975657+0.07224953423714474j)], [(-0.18002103957975657+0.07224953423714474j), (0.004722472920167423-0.9809944588837683j)]], dtype=np.complex128))],[(np.array([[(-0.0897362442086916+0.02445105905520244j), (-0.8503296766825128-0.5179662084918382j)], [(0.8503296766825128-0.5179662084918382j), (-0.0897362442086916-0.02445105905520244j)]], dtype=np.complex128), np.array([[(0.7441127742234152+0.6642425418602642j), (0.009545063892332065+0.07061810374004159j)], [(-0.009545063892332065+0.07061810374004159j), (0.7441127742234152-0.6642425418602642j)]], dtype=np.complex128))],[(np.array([[(-0.8113361037323902+0.388228151720065j), (0.38855300559639544-0.20009795309891054j)], [(-0.38855300559639544-0.20009795309891054j), (-0.8113361037323902-0.388228151720065j)]], dtype=np.complex128), np.array([[(-0.15500592487333173-0.5209557609876208j), (-0.6259723589563565+0.5592288119996914j)], [(0.6259723589563565+0.5592288119996914j), (-0.15500592487333173+0.5209557609876208j)]], dtype=np.complex128)),(np.array([[(-0.8113361037323902+0.388228151720065j), (0.38855300559639544-0.20009795309891054j)], [(-0.38855300559639544-0.20009795309891054j), (-0.8113361037323902-0.388228151720065j)]], dtype=np.complex128), np.array([[(-0.15500592487333173-0.5209557609876208j), (-0.6259723589563565+0.5592288119996914j)], [(0.6259723589563565+0.5592288119996914j), (-0.15500592487333173+0.5209557609876208j)]], dtype=np.complex128))],[(np.array([[(0.41295648139511504-0.17280776715854063j), (0.5430881450957462-0.7103940362502399j)], [(-0.5430881450957462-0.7103940362502399j), (0.41295648139511504+0.17280776715854063j)]], dtype=np.complex128), np.array([[(0.19013479709876246+0.4941656851052965j), (-0.6469746262664201+0.5487010730480224j)], [(0.6469746262664201+0.5487010730480224j), (0.19013479709876246-0.4941656851052965j)]], dtype=np.complex128)),(np.array([[(0.41295648139511504-0.17280776715854063j), (0.5430881450957462-0.7103940362502399j)], [(-0.5430881450957462-0.7103940362502399j), (0.41295648139511504+0.17280776715854063j)]], dtype=np.complex128), np.array([[(0.19013479709876246+0.4941656851052965j), (-0.6469746262664201+0.5487010730480224j)], [(0.6469746262664201+0.5487010730480224j), (0.19013479709876246-0.4941656851052965j)]], dtype=np.complex128))]], 0.49, 'Fraction of Weyl chamber reached with 2 gates: 0.600\nFraction of Weyl chamber reached with 2 gates and 3 gates(same single qubit): 1.000', ()) \ No newline at end of file diff --git a/cirq-google/cirq_google/optimizers/__init__.py b/cirq-google/cirq_google/optimizers/__init__.py index 1fa1603f7c7..07d3b9d721b 100644 --- a/cirq-google/cirq_google/optimizers/__init__.py +++ b/cirq-google/cirq_google/optimizers/__init__.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. """Package for optimizers and gate compilers related to Google-specific devices.""" -from cirq_google.optimizers.two_qubit_gates import ( - gate_product_tabulation, - GateTabulation, -) +# from cirq_google.optimizers.two_qubit_gates import ( +# gate_product_tabulation, +# GateTabulation, +# ) from cirq_google.optimizers.convert_to_sycamore_gates import ( ConvertToSycamoreGates, diff --git a/cirq-google/cirq_google/optimizers/convert_to_sycamore_gates.py b/cirq-google/cirq_google/optimizers/convert_to_sycamore_gates.py index 52ecc6acd3d..b9e2dcec4b7 100644 --- a/cirq-google/cirq_google/optimizers/convert_to_sycamore_gates.py +++ b/cirq-google/cirq_google/optimizers/convert_to_sycamore_gates.py @@ -20,7 +20,6 @@ import cirq import cirq_google as google from cirq_google.ops import SycamoreGate -from cirq_google.optimizers.two_qubit_gates.gate_compilation import GateTabulation UNITARY_ZZ = np.kron(cirq.unitary(cirq.Z), cirq.unitary(cirq.Z)) @@ -49,7 +48,9 @@ class ConvertToSycamoreGates(cirq.PointOptimizer): # TODO(#3388) Add documentation for Raises. # pylint: disable=missing-raises-doc - def __init__(self, tabulation: Optional[GateTabulation] = None, ignore_failures=False) -> None: + def __init__( + self, tabulation: Optional['cirq.GateTabulation'] = None, ignore_failures=False + ) -> None: """Inits ConvertToSycamoreGates. Args: @@ -63,8 +64,8 @@ def __init__(self, tabulation: Optional[GateTabulation] = None, ignore_failures= """ super().__init__() self.ignore_failures = ignore_failures - if tabulation is not None and not isinstance(tabulation, GateTabulation): - raise ValueError("provided tabulation must be of type GateTabulation") + if tabulation is not None and not isinstance(tabulation, cirq.GateTabulation): + raise ValueError("provided tabulation must be of type cirq.GateTabulation") self.tabulation = tabulation # pylint: enable=missing-raises-doc @@ -182,7 +183,7 @@ def known_two_q_operations_to_sycamore_operations( qubit_a: cirq.Qid, qubit_b: cirq.Qid, op: cirq.Operation, - tabulation: Optional[GateTabulation] = None, + tabulation: Optional['cirq.GateTabulation'] = None, ) -> cirq.OP_TREE: """Synthesizes a known gate operation to a sycamore operation. @@ -315,7 +316,7 @@ def decompose_phased_iswap_into_syc_precomputed( def decompose_arbitrary_into_syc_tabulation( - qubit_a: cirq.Qid, qubit_b: cirq.Qid, op: cirq.Operation, tabulation: GateTabulation + qubit_a: cirq.Qid, qubit_b: cirq.Qid, op: cirq.Operation, tabulation: cirq.GateTabulation ) -> cirq.OP_TREE: """Synthesize an arbitrary 2 qubit operation to a sycamore operation using the given Tabulation. diff --git a/cirq-google/cirq_google/optimizers/convert_to_sycamore_gates_test.py b/cirq-google/cirq_google/optimizers/convert_to_sycamore_gates_test.py index ed29bdbda7f..02c28b8eee3 100644 --- a/cirq-google/cirq_google/optimizers/convert_to_sycamore_gates_test.py +++ b/cirq-google/cirq_google/optimizers/convert_to_sycamore_gates_test.py @@ -6,7 +6,6 @@ import cirq import cirq_google import cirq_google.optimizers.convert_to_sycamore_gates as cgoc -from cirq_google.optimizers.two_qubit_gates.gate_compilation import gate_product_tabulation _rng = cirq.value.parse_random_state(11) # for determinism @@ -261,7 +260,7 @@ def test_convert_to_sycamore_equivalent_unitaries(gate): def test_convert_to_sycamore_tabulation(): # A tabulation for the sycamore gate with an infidelity of .1. - sycamore_tabulation = gate_product_tabulation( + sycamore_tabulation = cirq.gate_product_tabulation( cirq.unitary(cirq_google.SYC), 0.1, random_state=_rng ) qubits = [cirq.NamedQubit('a'), cirq.NamedQubit('b')] diff --git a/cirq-google/cirq_google/optimizers/optimize_for_sycamore.py b/cirq-google/cirq_google/optimizers/optimize_for_sycamore.py index 68a6b6e2722..68fcd604feb 100644 --- a/cirq-google/cirq_google/optimizers/optimize_for_sycamore.py +++ b/cirq-google/cirq_google/optimizers/optimize_for_sycamore.py @@ -23,8 +23,6 @@ convert_to_xmon_gates, ConvertToSycamoreGates, ConvertToSqrtIswapGates, - gate_product_tabulation, - GateTabulation, ) if TYPE_CHECKING: @@ -40,7 +38,7 @@ def _get_common_cleanup_optimizers(tolerance: float) -> List[Callable[[cirq.Circ def _get_xmon_optimizers( - tolerance: float, tabulation: Optional[GateTabulation] + tolerance: float, tabulation: Optional['cirq.GateTabulation'] ) -> List[Callable[[cirq.Circuit], None]]: if tabulation is not None: # coverage: ignore @@ -55,7 +53,7 @@ def _get_xmon_optimizers( def _get_xmon_optimizers_part_cz( - tolerance: float, tabulation: Optional[GateTabulation] + tolerance: float, tabulation: Optional['cirq.GateTabulation'] ) -> List[Callable[[cirq.Circuit], None]]: if tabulation is not None: # coverage: ignore @@ -69,7 +67,7 @@ def _get_xmon_optimizers_part_cz( def _get_sycamore_optimizers( - tolerance: float, tabulation: Optional[GateTabulation] + tolerance: float, tabulation: Optional['cirq.GateTabulation'] ) -> List[Callable[[cirq.Circuit], None]]: return [ ConvertToSycamoreGates(tabulation=tabulation).optimize_circuit, @@ -79,7 +77,7 @@ def _get_sycamore_optimizers( def _get_sqrt_iswap_optimizers( - tolerance: float, tabulation: Optional[GateTabulation] + tolerance: float, tabulation: Optional['cirq.GateTabulation'] ) -> List[Callable[[cirq.Circuit], None]]: if tabulation is not None: # coverage: ignore @@ -102,10 +100,10 @@ def _get_sqrt_iswap_optimizers( @lru_cache() def _gate_product_tabulation_cached( optimizer_type: str, tabulation_resolution: float -) -> GateTabulation: +) -> cirq.GateTabulation: random_state = np.random.RandomState(51) if optimizer_type == 'sycamore': - return gate_product_tabulation( + return cirq.gate_product_tabulation( cirq.unitary(cg_ops.SYC), tabulation_resolution, random_state=random_state ) else: @@ -154,7 +152,7 @@ def optimized_for_sycamore( f'types are: {_OPTIMIZER_TYPES.keys()}' ) - tabulation: Optional[GateTabulation] = None + tabulation: Optional['cirq.GateTabulation'] = None if tabulation_resolution is not None: tabulation = _gate_product_tabulation_cached(optimizer_type, tabulation_resolution) diff --git a/cirq-google/cirq_google/optimizers/two_qubit_gates/__init__.py b/cirq-google/cirq_google/optimizers/two_qubit_gates/__init__.py deleted file mode 100644 index 29611b7ea2c..00000000000 --- a/cirq-google/cirq_google/optimizers/two_qubit_gates/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -from cirq_google.optimizers.two_qubit_gates.gate_compilation import ( - gate_product_tabulation, - GateTabulation, -) diff --git a/cirq-google/cirq_google/optimizers/two_qubit_gates/example.py b/cirq-google/cirq_google/optimizers/two_qubit_gates/example.py index 5c971718adf..d89ccffbcf5 100644 --- a/cirq-google/cirq_google/optimizers/two_qubit_gates/example.py +++ b/cirq-google/cirq_google/optimizers/two_qubit_gates/example.py @@ -23,8 +23,7 @@ from matplotlib import pyplot as plt import cirq -from cirq_google.optimizers.two_qubit_gates.gate_compilation import gate_product_tabulation -from cirq_google.optimizers.two_qubit_gates.math_utils import unitary_entanglement_fidelity +from cirq.optimizers.two_qubit_gate_math_utils import unitary_entanglement_fidelity from cirq.testing import random_special_unitary @@ -49,7 +48,7 @@ def main(samples: int = 1000, max_infidelity: float = 0.01): # typical distance between an arbitrary two-qubit gate and the nearest # tabulated gate. start = time() - tabulation = gate_product_tabulation(base, max_infidelity) + tabulation = cirq.gate_product_tabulation(base, max_infidelity) print(tabulation.summary) print(f'Gate tabulation time : {time() - start} seconds.')