def __call__(self, state: tf.Tensor, is_density_matrix: bool = False) -> tf.Tensor: TensorflowGate.__call__(self, state, is_density_matrix) if self.rank == 1: return op.apply_gate(state, self.matrix, self.qubits_tensor, self.nqubits, self.target_qubits[0]) if self.rank == 2: return op.apply_two_qubit_gate(state, self.matrix, self.qubits_tensor, self.nqubits, *self.target_qubits)
def test_apply_pauli_gate(nqubits, target, gate, compile): """Check ``apply_x``, ``apply_y`` and ``apply_z`` kernels.""" matrices = {"x": np.array([[0, 1], [1, 0]], dtype=np.complex128), "y": np.array([[0, -1j], [1j, 0]], dtype=np.complex128), "z": np.array([[1, 0], [0, -1]], dtype=np.complex128)} state = utils.random_tensorflow_complex((2 ** nqubits,), dtype=tf.float64) target_state = tf.cast(state.numpy(), dtype=state.dtype) qubits = qubits_tensor(nqubits, [target]) target_state = op.apply_gate(state, matrices[gate], qubits, nqubits, target, get_threads()) def apply_operator(state): qubits = qubits_tensor(nqubits, [target]) return getattr(op, "apply_{}".format(gate))(state, qubits, nqubits, target, get_threads()) if compile: apply_operator = tf.function(apply_operator) state = apply_operator(state) np.testing.assert_allclose(target_state.numpy(), state.numpy())
def apply_operator(state): qubits = qubits_tensor(nqubits, [nqubits - 1], controls) return op.apply_gate(state, xgate, qubits, nqubits, nqubits - 1)
def apply_operator(state, gate): qubits = qubits_tensor(nqubits, [target]) return op.apply_gate(state, gate, qubits, nqubits, target)
def __call__(self, state: tf.Tensor, is_density_matrix: bool = False) -> tf.Tensor: super(MatrixGate, self).__call__(state, is_density_matrix) return op.apply_gate(state, self.matrix, self.qubits_tensor, self.nqubits, self.target_qubits[0])
def apply_operator(state): qubits = qubits_tensor(nqubits, [target], controls) return op.apply_gate(state, gate, qubits, nqubits, target, get_threads())