def _random_double_partial_cz_effect(): return linalg.dot( linalg.kron(testing.random_unitary(2), testing.random_unitary(2)), np.diag([1, 1, 1, cmath.exp(2j * random.random() * np.pi)]), linalg.kron(testing.random_unitary(2), testing.random_unitary(2)), np.diag([1, 1, 1, cmath.exp(2j * random.random() * np.pi)]), linalg.kron(testing.random_unitary(2), testing.random_unitary(2)))
def _random_double_full_cz_effect(): return linalg.dot( linalg.kron(testing.random_unitary(2), testing.random_unitary(2)), ops.CZ.matrix(), linalg.kron(testing.random_unitary(2), testing.random_unitary(2)), ops.CZ.matrix(), linalg.kron(testing.random_unitary(2), testing.random_unitary(2)))
def test_kron_factor_special_unitaries(f1, f2): p = linalg.kron(f1, f2) g, g1, g2 = linalg.kron_factor_4x4_to_2x2s(p) assert np.allclose(linalg.kron(g1, g2), p) assert abs(g - 1) < 0.000001 assert linalg.is_special_unitary(g1) assert linalg.is_special_unitary(g2)
def recompose_kak(g, a, v, b) -> np.ndarray: a1, a0 = a x, y, z = v b1, b0 = b xx = linalg.kron(X, X) yy = linalg.kron(Y, Y) zz = linalg.kron(Z, Z) a = linalg.kron(a1, a0) m = linalg.map_eigenvalues(xx * x + yy * y + zz * z, lambda e: np.exp(1j * e)) b = linalg.kron(b1, b0) return linalg.dot(a, m, b) * g
def recompose_so4(a: np.ndarray, b: np.ndarray) -> np.ndarray: assert a.shape == (2, 2) assert b.shape == (2, 2) assert linalg.is_special_unitary(a) assert linalg.is_special_unitary(b) magic = np.array([[1, 0, 0, 1j], [0, 1j, 1, 0], [0, 1j, -1, 0], [1, 0, 0, -1j]]) * np.sqrt(0.5) result = np.real( combinators.dot(np.conj(magic.T), linalg.kron(a, b), magic)) assert linalg.is_orthogonal(result) return result
def matrix(self, qubits: Optional[Iterable[raw_types.Qid]] = None) -> np.ndarray: """Returns the matrix of self in computational basis of qubits. Args: qubits: Ordered collection of qubits that determine the subspace in which the matrix representation of the Pauli string is to be computed. Qubits absent from self.qubits are acted on by the identity. Defaults to self.qubits. """ qubits = self.qubits if qubits is None else qubits factors = [self.get(q, default=identity.I) for q in qubits] return linalg.kron(self.coefficient, *[protocols.unitary(f) for f in factors])
def _unitary_(self) -> Optional[np.ndarray]: if not self._has_unitary_(): return None return linalg.kron(self.coefficient, *[protocols.unitary(self[q]) for q in self.qubits])
def _unitary_(self): if not self._has_unitary_(): return NotImplemented return self.coefficient * linalg.kron( *[protocols.unitary(PAULI_GATES[p]) for p in self.pauli_mask])
# # 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 numpy as np import pytest import cirq from cirq import Symbol, linalg, Circuit from cirq.testing import EqualsTester H = np.array([[1, 1], [1, -1]]) * np.sqrt(0.5) HH = linalg.kron(H, H) QFT2 = np.array([[1, 1, 1, 1], [1, 1j, -1, -1j], [1, -1, 1, -1], [1, -1j, -1, 1j]]) * 0.5 def test_cz_init(): assert cirq.Rot11Gate(half_turns=0.5).half_turns == 0.5 assert cirq.Rot11Gate(half_turns=5).half_turns == 1 def test_cz_str(): assert str(cirq.Rot11Gate()) == 'CZ' assert str(cirq.Rot11Gate(half_turns=0.5)) == 'CZ**0.5' assert str(cirq.Rot11Gate(half_turns=-0.25)) == 'CZ**-0.25'
def test_kron_factor(f1, f2): p = linalg.kron(f1, f2) g, g1, g2 = linalg.kron_factor_4x4_to_2x2s(p) assert abs(np.linalg.det(g1) - 1) < 0.00001 assert abs(np.linalg.det(g2) - 1) < 0.00001 assert np.allclose(g * linalg.kron(g1, g2), p)
X = np.array([[0, 1], [1, 0]]) Y = np.array([[0, -1j], [1j, 0]]) Z = np.array([[1, 0], [0, -1]]) H = np.array([[1, 1], [1, -1]]) * np.sqrt(0.5) SQRT_X = np.array([[1, 1j], [1j, 1]]) c = np.exp(1j * np.pi / 4) SQRT_SQRT_X = np.array([[1 + c, 1 - c], [1 - c, 1 + c]]) / 2 SWAP = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]) CNOT = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) CZ = np.diag([1, 1, 1, -1]) @pytest.mark.parametrize( 'matrix', [X, linalg.kron(X, X), linalg.kron(X, Y), linalg.kron(X, np.eye(2))]) def test_map_eigenvalues_identity(matrix): identity_mapped = linalg.map_eigenvalues(matrix, lambda e: e) assert np.allclose(matrix, identity_mapped) @pytest.mark.parametrize('matrix,exponent,desired', [ [X, 2, np.eye(2)], [X, 3, X], [Z, 2, np.eye(2)], [H, 2, np.eye(2)], [Z, 0.5, np.diag([1, 1j])], [X, 0.5, np.array([[1j, 1], [1, 1j]]) * (1 - 1j) / 2], ])