コード例 #1
0
ファイル: gate_wrappers_test.py プロジェクト: peterse/tf-cirq
def test_tf_gate_wrapper_gate_inheritance_parametrized(g):
    inst = g(0.01)(q(0))
    wrapped = tf_gate_wrapper(inst)
    unitary = cirq.unitary(inst)
    wrapped = tf_gate_wrapper(inst, tf.complex64)
    with tf.Session() as sess:
        tf_inst = sess.run(wrapped._tensor)
    np.testing.assert_array_almost_equal(cirq.unitary(inst), tf_inst)
コード例 #2
0
ファイル: gate_wrappers_test.py プロジェクト: peterse/tf-cirq
def test_tf_gate_wrapper_tensor_inputs():
    # TODO
    init_t = np.pi / 2
    t = tf.Variable(init_t)
    inst = cirq.YPowGate(exponent=t)(q(0))
    wrapped = tf_gate_wrapper(inst)
    print(wrapped._tensor)
コード例 #3
0
ファイル: gate_wrappers_test.py プロジェクト: peterse/tf-cirq
def test_tf_gate_wrapper_parity_gate():
    for g in [cirq.ZZPowGate]:
        inst = g(exponent=3.84)(q(0), q(1))
        wrapped = tf_gate_wrapper(inst)
        with tf.Session() as sess:
            tf_inst = sess.run(wrapped._tensor).reshape((4, 4))
        np.testing.assert_array_almost_equal(cirq.unitary(inst), tf_inst)
コード例 #4
0
ファイル: gate_wrappers_test.py プロジェクト: peterse/tf-cirq
def test_tf_gate_wrapper_gate_inheritance(g):
    """Wrapping a gate instance with a wrapper base class."""
    inst = g(q(0))
    wrapped = tf_gate_wrapper(inst, tf.complex64)
    with tf.Session() as sess:
        tf_inst = sess.run(wrapped._tensor)
    np.testing.assert_array_almost_equal(cirq.unitary(inst), tf_inst)
コード例 #5
0
ファイル: tf_simulator.py プロジェクト: peterse/tf-cirq
    def simulate_sweep(
            self,
            program: Union[circuits.Circuit],
            params: study.Sweepable,
            qubit_order: ops.QubitOrderOrList = ops.QubitOrder.DEFAULT,
            initial_state: Any = None) -> List[tf.Tensor]:
        """Simulates the supplied Circuit or Schedule.

        Note: This differs from cirq.WaveFunctionSimulator in the return
        type, which is List[tf.tensor] instead of List[SimulationTrialResult].

        This method returns a result which allows access to the entire
        wave function. In contrast to simulate, this allows for sweeping
        over different parameter values.

        Args:
            program: The circuit or schedule to simulate.
            params: Parameters to run with the program.
            qubit_order: Determines the canonical ordering of the qubits. This
                is often used in specifying the initial state, i.e. the
                ordering of the computational basis states.
            initial_state: The initial state for the simulation. The form of
                this state depends on the simulation implementation.  See
                documentation of the implementing class for details.

        Returns:
            List of wavefunctions for this run, one for each possible parameter
                resolver.
        """
        # 1. checking/promotion of initial state to track qubits
        # TODO: enforce sizing on initial_state; don't want any stray/missing qubits
        if initial_state is None:
            initial_state = np.zeros((2**len(program.all_qubits()), ))
            initial_state[0] = 1
        if isinstance(initial_state, np.ndarray):
            initial_qubits = range(int(np.log2(initial_state.shape[0])))
            # enforce tensor shape 2,2,2,2,...
            initial_state = initial_state.reshape((2, ) * len(initial_qubits))
            state = tf.convert_to_tensor(value=initial_state,
                                         dtype=self._dtype)
            # fixme: util here:
            # state = TFWaveFunctionSimulatorState(state, initial_qubits)

        # TODO: gather a set of all qubits acted on in this circuit

        # FIXME: initialize buffer variable
        # buf = tf.Variable(tf.zeros_like(state, dtype=self._dtype), name='buffer')
        # Moment-wise construction of a set of matrices to apply wall
        self.ops = []
        self.indices = []
        for moment in program:
            # FIXME: empty moment?
            for op in moment.operations:
                self.ops.append(tf_gate_wrapper(op, dtype=self._dtype))
                self.indices.append([q.x for q in op.qubits])

        param_resolvers = study.to_resolvers(params)
        trial_results = []
        qubit_order = ops.QubitOrder.as_qubit_order(qubit_order)
        for param_resolver in param_resolvers:

            # prepare to iteratively construct graph down the line of ops
            state_and_buff = _StateAndBuffer(state, None)  # initializer
            for op, inds in zip(self.ops, self.indices):
                # can't flush the buffer!!
                # buf = tf.assign(tf.get_variable(name='buffer'), tf.zeros_like(state, dtype=self._dtype))
                buf = tf.zeros_like(state, dtype=self._dtype)
                state_and_buff = _StateAndBuffer(state_and_buff.state, buf)
                state_and_buff = self._simulate_unitary(
                    op, state_and_buff, inds)

            # TODO: actually make sweep usable by supplying parameters here..?
            # trial_results.append(
            #     SimulationTrialResult(
            #         params=param_resolver,
            #         measurements={},
            #         final_simulator_state=state_and_buff.state))
            trial_results.append(state_and_buff.state)

        return trial_results
コード例 #6
0
ファイル: gate_wrappers_test.py プロジェクト: peterse/tf-cirq
def test_tf_gate_wrapper_single_qubit_eigengate(g):
    inst = g(exponent=3.84)(q(0), q(1))
    wrapped = tf_gate_wrapper(inst)
    with tf.Session() as sess:
        tf_inst = sess.run(wrapped._tensor).reshape((4, 4))
    np.testing.assert_array_almost_equal(cirq.unitary(inst), tf_inst)
コード例 #7
0
ファイル: gate_wrappers_test.py プロジェクト: peterse/tf-cirq
def test_tf_gate_wrapper_single_qubit_eigengate(g):
    inst = g(exponent=1.5)(q(0))
    wrapped = tf_gate_wrapper(inst, tf.complex64)
    with tf.Session() as sess:
        tf_inst = sess.run(wrapped._tensor)
    np.testing.assert_array_almost_equal(cirq.unitary(inst), tf_inst)
コード例 #8
0
ファイル: gate_wrappers_test.py プロジェクト: peterse/tf-cirq
def test_tf_gate_wrapper_gate_inheritance(g):
    inst = g(q(0), q(1))
    wrapped = tf_gate_wrapper(inst, tf.complex64)
    with tf.Session() as sess:
        tf_inst = sess.run(wrapped._tensor).reshape((4, 4))
    np.testing.assert_array_almost_equal(cirq.unitary(inst), tf_inst)