tensorflow / quantum

Hybrid Quantum-Classical Machine Learning in TensorFlow

Home Page:https://www.tensorflow.org/quantum

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to return just the output of the quantum circuit without any operators involved?

Shuhul24 opened this issue · comments

I am using tfq.layers.PQC and tfq.layers.ControlledPQC extensively, but say I don't want to check the expectation value of the qubit state rather just want to check what's the output of the state (might be in tf.Tensor form). In tfq.layers.PQC (and tfq.layers.ControlledPQC) we have got an attribute by the name of operators which measures the expectation value of the qubit state in Z basis, if the operator is chosen to be cirq.Z(cirq.GridQubit))?
I tried using operators = [] but ended up having an error which is probably related to this operators attribute.

[/usr/local/lib/python3.8/dist-packages/tensorflow/python/autograph/impl/api.py](https://localhost:8080/#) in wrapper(*args, **kwargs)
    697       except Exception as e:  # pylint:disable=broad-except
    698         if hasattr(e, 'ag_error_metadata'):
--> 699           raise e.ag_error_metadata.to_exception(e)
    700         else:
    701           raise

TypeError: Exception encountered when calling layer "discriminator_3" (type Discriminator).

in user code:

    File "<ipython-input-46-be27fbd4a19d>", line 30, in call  *
        quantum_output = self.quantum_operation([tiled_circuits, quantum_inputs])
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler  **
        raise e.with_traceback(filtered_tb) from None

    TypeError: Exception encountered when calling layer "controlled_pqc_5" (type ControlledPQC).
    
    in user code:
    
        File "/usr/local/lib/python3.8/dist-packages/tensorflow_quantum/python/layers/high_level/controlled_pqc.py", line 264, in call  *
            return self._layer(model_appended,
        File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler  **
            raise e.with_traceback(filtered_tb) from None
    
        TypeError: Exception encountered when calling layer "expectation_5" (type Expectation).
        
        in user code:
        
            File "/usr/local/lib/python3.8/dist-packages/tensorflow_quantum/python/layers/circuit_executors/expectation.py", line 357, in call  *
                operators)
            File "/usr/local/lib/python3.8/dist-packages/tensorflow_quantum/python/differentiators/differentiator.py", line 156, in op_wrapper_analytic
                forward_pass_vals = analytic_op(programs, symbol_names,
            File "/usr/local/lib/python3.8/dist-packages/tensorflow_quantum/core/ops/circuit_execution_ops.py", line 135, in <lambda>
                op(programs, symbol_names, symbol_values, pauli_sums)
            File "/usr/local/lib/python3.8/dist-packages/tensorflow_quantum/core/ops/tfq_simulate_ops.py", line 44, in tfq_simulate_expectation
                return SIM_OP_MODULE.tfq_simulate_expectation(
            File "<string>", line 79, in tfq_simulate_expectation
                
        
            TypeError: Input 'pauli_sums' of 'TfqSimulateExpectation' Op has type float32 that does not match expected type of string.
        
        
        Call arguments received:
          • inputs=tf.Tensor(shape=(None,), dtype=string)
          • symbol_names=tf.Tensor(shape=(22,), dtype=string)
          • symbol_values=tf.Tensor(shape=(None, 22), dtype=float32)
          • operators=tf.Tensor(shape=(None, 0), dtype=float32)
          • repetitions=Noneinitializer=<keras.initializers.initializers_v2.RandomUniform object at 0x7f856033a700>
    
    
    Call arguments received:
      • inputs=['tf.Tensor(shape=(None,), dtype=string)', 'tf.Tensor(shape=(None, 22), dtype=float32)']


Call arguments received:
  • inputs=tf.Tensor(shape=(None, 2), dtype=float32)

You can use tfq.layers.State to check the state

Is it possible that we can use the output of tfq.layers.State as an input to any of the tfq.layers? Also, how can we measure the output of tfq.layers.State using an observable?

I think state takes the circuit and parameters as input, so you would want to extract those from the PQC (you can check the docs to see if I'm right). If you want the observable, why are you using state? PQC and ControlledPQC computer observables for you. If you really must, you could of course just compute <psi|O|psi>.