def test_xxyy_matrix(): numpy.testing.assert_allclose(XXYYGate(half_turns=2).matrix(), numpy.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]]), atol=1e-8) numpy.testing.assert_allclose(XXYYGate(half_turns=1).matrix(), numpy.array([[1, 0, 0, 0], [0, 0, -1j, 0], [0, -1j, 0, 0], [0, 0, 0, 1]]), atol=1e-8) numpy.testing.assert_allclose(XXYYGate(half_turns=0).matrix(), numpy.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]), atol=1e-8) numpy.testing.assert_allclose(XXYYGate(half_turns=-1).matrix(), numpy.array([[1, 0, 0, 0], [0, 0, 1j, 0], [0, 1j, 0, 0], [0, 0, 0, 1]]), atol=1e-8) X = numpy.array([[0, 1], [1, 0]]) Y = numpy.array([[0, -1j], [1j, 0]]) XX = kron(X, X) YY = kron(Y, Y) numpy.testing.assert_allclose(XXYYGate(half_turns=0.25).matrix(), expm(-1j * numpy.pi * 0.25 * (XX + YY) / 4))
def one_and_two_body_interaction(p, q, a, b) -> cirq.OP_TREE: yield XXYYGate(duration=self.hamiltonian.one_body[p, q].real * time).on(a, b) yield YXXYGate(duration=self.hamiltonian.one_body[p, q].imag * time).on(a, b) yield cirq.Rot11Gate(rads=-2 * self.hamiltonian.two_body[p, q] * time).on(a, b)
def one_and_two_body_interaction(p, q, a, b) -> cirq.OP_TREE: t_symbol = LetterWithSubscripts('T', p, q, i) w_symbol = LetterWithSubscripts('W', p, q, i) v_symbol = LetterWithSubscripts('V', p, q, i) if t_symbol in param_set: yield XXYYGate(half_turns=t_symbol).on(a, b) if w_symbol in param_set: yield YXXYGate(half_turns=w_symbol).on(a, b) if v_symbol in param_set: yield cirq.Rot11Gate(half_turns=v_symbol).on(a, b)
def one_and_two_body_interaction(p, q, a, b) -> cirq.OP_TREE: if 'T{}_{}'.format(p, q) + suffix in self.params: yield XXYYGate(half_turns=self.params[ 'T{}_{}'.format(p, q) + suffix]).on(a, b) if 'W{}_{}'.format(p, q) + suffix in self.params: yield YXXYGate(half_turns=self.params[ 'W{}_{}'.format(p, q) + suffix]).on(a, b) if 'V{}_{}'.format(p, q) + suffix in self.params: yield cirq.Rot11Gate(half_turns=self.params[ 'V{}_{}'.format(p, q) + suffix]).on(a, b)
def test_xxyy_repr(): assert repr(XXYYGate(half_turns=1)) == 'XXYY' assert repr(XXYYGate(half_turns=0.5)) == 'XXYY**0.5'
def test_xxyy_eq(): eq = EqualsTester() eq.add_equality_group(XXYYGate(half_turns=3.5), XXYYGate(half_turns=-0.5), XXYYGate(rads=-0.5 * numpy.pi), XXYYGate(degs=-90), XXYYGate(duration=-0.5 * numpy.pi / 2)) eq.add_equality_group(XXYYGate(half_turns=1.5), XXYYGate(half_turns=-2.5), XXYYGate(rads=1.5 * numpy.pi), XXYYGate(degs=-450), XXYYGate(duration=-2.5 * numpy.pi / 2)) eq.make_equality_group(lambda: XXYYGate(half_turns=0)) eq.make_equality_group(lambda: XXYYGate(half_turns=0.5))
def test_xxyy_init_with_multiple_args_fails(): with pytest.raises(ValueError): _ = XXYYGate(half_turns=1.0, duration=numpy.pi/2)
def test_xxyy_init(): assert XXYYGate(half_turns=0.5).half_turns == 0.5 assert XXYYGate(half_turns=1.5).half_turns == 1.5 assert XXYYGate(half_turns=5).half_turns == 1
def trotter_step( self, qubits: Sequence[cirq.QubitId], time: float, control_qubit: Optional[cirq.QubitId]=None ) -> cirq.OP_TREE: n_qubits = len(qubits) # Simulate the off-diagonal one-body terms. yield swap_network( qubits, lambda p, q, a, b: XXYYGate(duration= self.one_body_coefficients[p, q].real * time).on(a, b), fermionic=True) qubits = qubits[::-1] # Simulate the diagonal one-body terms. yield (cirq.RotZGate(rads= -self.one_body_coefficients[j, j].real * time).on(qubits[j]) for j in range(n_qubits)) # Simulate each singular vector of the two-body terms. prior_basis_matrix = numpy.identity(n_qubits, complex) for j in range(len(self.eigenvalues)): # Get the basis change matrix and its inverse. two_body_coefficients = self.scaled_density_density_matrices[j] basis_change_matrix = self.basis_change_matrices[j] # If the basis change matrix is unitary, merge rotations. # Otherwise, you must simulate two basis rotations. is_unitary = cirq.is_unitary(basis_change_matrix) if is_unitary: inverse_basis_matrix = numpy.conjugate( numpy.transpose(basis_change_matrix)) merged_basis_matrix = numpy.dot(prior_basis_matrix, inverse_basis_matrix) yield bogoliubov_transform(qubits, merged_basis_matrix) else: # TODO add LiH test to cover this. # coverage: ignore if j: yield bogoliubov_transform(qubits, prior_basis_matrix) yield cirq.inverse( bogoliubov_transform(qubits, basis_change_matrix)) # Simulate the off-diagonal two-body terms. yield swap_network( qubits, lambda p, q, a, b: cirq.Rot11Gate(rads= -2 * two_body_coefficients[p, q] * time).on(a, b)) qubits = qubits[::-1] # Simulate the diagonal two-body terms. yield (cirq.RotZGate(rads= -two_body_coefficients[k, k] * time).on(qubits[k]) for k in range(n_qubits)) # Undo basis transformation non-unitary case. # Else, set basis change matrix to prior matrix for merging. if is_unitary: prior_basis_matrix = basis_change_matrix else: # TODO add LiH test to cover this. # coverage: ignore yield bogoliubov_transform(qubits, basis_change_matrix) prior_basis_matrix = numpy.identity(n_qubits, complex) # Undo final basis transformation in unitary case. if is_unitary: yield bogoliubov_transform(qubits, prior_basis_matrix)