# Any thought on I11.4

**URL:** <https://discuss.pennylane.ai/t/any-thought-on-i11-4/1510>\
**Category:** Codebook\
**Created:** [November 25, 2021, 6:32am UTC](https://discuss.pennylane.ai/t/any-thought-on-i11-4/1510 "2021-11-25T06:32:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vedant\_Dwivedi](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vedant_dwivedi/32/813_2.png) [@Vedant\_Dwivedi](https://discuss.pennylane.ai/u/Vedant_Dwivedi)\
**Post date:** [November 25, 2021, 6:32am UTC](https://discuss.pennylane.ai/t/any-thought-on-i11-4/1510/1 "2021-11-25T06:32:16Z")

</div>

dev = qml.device(‘default.qubit’, wires=2)

@qml.qnode(dev)  
def circuit\_1(theta):  
“”"Implement the circuit and measure Z I and I Z.

```
Args:
    theta (float): a rotation angle.
    
Returns:
    float, float: The expectation values of the observables Z I, and I Z
"""
##################
# YOUR CODE HERE #
##################  
qml.RX(2*theta,wires=0)
qml.RY(4*theta,wires=1)
return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))

```

@qml.qnode(dev)  
def circuit\_2(theta):  
“”"Implement the circuit and measure Z Z.

```
Args:
    theta (float): a rotation angle.
    
Returns:
    float: The expectation value of the observable Z Z
""" 

##################
# YOUR CODE HERE #
##################  
qml.RX(2*theta,wires=0)
qml.RY(4*theta,wires=1)

return qml.expval(qml.PauliZ(0)@qml.PauliZ(1))

```

def zi\_iz\_combination(ZI\_results, IZ\_results):  
“”"Implement a function that acts on the ZI and IZ results to  
produce the ZZ results. How do you think they should combine?

```
Args:
    ZI_results (array[float]): Results from the expectation value of 
        ZI in circuit_1.
    IZ_results (array[float]): Results from the expectation value of 
        IZ in circuit_2.

Returns:
    array[float]: A combination of ZI_results and IZ_results that 
    produces results equivalent to measuring ZZ.
"""

combined_results=np.zeros(len(ZI_results))

##################
# YOUR CODE HERE #
##################

b=np.array(ZI_results)
c=np.array(IZ_results)

Z=np.kron(b,c)
combined_results.append(Z)

return combined_results

```

theta = np.linspace(0, 2 \* np.pi, 100)

# Run circuit 1, and process the results

circuit\_1\_results = np.array([circuit\_1(t) for t in theta])

ZI\_results = circuit\_1\_results[:, 0]  
IZ\_results = circuit\_1\_results[:, 1]  
combined\_results = zi\_iz\_combination(ZI\_results, IZ\_results)

# Run circuit 2

ZZ\_results = np.array([circuit\_2(t) for t in theta])

# Plot your results

plot = plotter(theta, ZI\_results, IZ\_results, ZZ\_results, combined\_results)

---

<div class="post-metadata">

**Author:** ![Vedant\_Dwivedi](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vedant_dwivedi/32/813_2.png) [@Vedant\_Dwivedi](https://discuss.pennylane.ai/u/Vedant_Dwivedi)\
**Post date:** [November 25, 2021, 7:30am UTC](https://discuss.pennylane.ai/t/any-thought-on-i11-4/1510/2 "2021-11-25T07:30:46Z")

</div>

if I increases Len by 2 it doesn’t work to accommodate the tensor product combined\_results=np.zeros(2\*len(ZI\_results))

---

<div class="post-metadata">

**Author:** ![glassnotes](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/glassnotes/32/327_2.png) [@glassnotes](https://discuss.pennylane.ai/u/glassnotes)\
**Post date:** [November 25, 2021, 1:39pm UTC](https://discuss.pennylane.ai/t/any-thought-on-i11-4/1510/3 "2021-11-25T13:39:16Z")

</div>

Hi @Vedant_Dwivedi, thanks for your question! You’re on the right track, but let’s take a closer look at the data types in the `zi_iz_combination` function.

```python
def zi_iz_combination(ZI_results, IZ_results):
    "”"Implement a function that acts on the ZI and IZ results to
    produce the ZZ results. How do you think they should combine?

    Args:
        ZI_results (array[float]): Results from the expectation value of 
            ZI in circuit_1.
        IZ_results (array[float]): Results from the expectation value of 
            IZ in circuit_2.

    Returns:
        array[float]: A combination of ZI_results and IZ_results that 
        produces results equivalent to measuring ZZ.
    """

```

[EDIT: ~~While it’s true that the _observables_ `ZI` and `IZ` need to be tensored together to produce the observable `ZZ`~~] The _expectation values_ being input to this function are just lists (arrays) of floats, and this function must return an array of floats of the same shape of the two inputs arrays. In the code snippet you posted, the two arrays are being tensored together using `np.kron`:

```python
b=np.array(ZI_results)
c=np.array(IZ_results)
Z=np.kron(b,c)

```

Is the shape of `Z` the same as `ZI_results` and `IZ_results`? (It might be helpful to try a small test example locally to see what happens here). What else might you do with the two arrays?

Hope that helps, please let us know if you have any further questions!

---

<div class="post-metadata">

**Author:** ![Vedant\_Dwivedi](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vedant_dwivedi/32/813_2.png) [@Vedant\_Dwivedi](https://discuss.pennylane.ai/u/Vedant_Dwivedi)\
**Post date:** [November 25, 2021, 2:21pm UTC](https://discuss.pennylane.ai/t/any-thought-on-i11-4/1510/4 "2021-11-25T14:21:26Z")

</div>

hoped I understood correctly but still no correct answer.

combined\_results=np.zeros(2\*len(ZI\_results))

```
##################
# YOUR CODE HERE #
##################
b=np.array(ZI_results)
c=np.array(IZ_results)
combined_results=np.kron(b,c)
```

---

<div class="post-metadata">

**Author:** ![glassnotes](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/glassnotes/32/327_2.png) [@glassnotes](https://discuss.pennylane.ai/u/glassnotes)\
**Post date:** [November 25, 2021, 7:53pm UTC](https://discuss.pennylane.ai/t/any-thought-on-i11-4/1510/5 "2021-11-25T19:53:01Z")

</div>

Hi @Vedant_Dwivedi,

The aim here is, given `ZI_results` and `IZ_results`, to combine them (mathematically) to create an array of the _same_ length that represents the `ZZ` results. What the function returns must have the same dimension as the input;

```auto
combined_results=np.zeros(2*len(ZI_results))

```

will give a result with twice the length of the inputs (and using `kron` will similarly give something with twice the length).

[BTW I have made an error in my earlier post, which I have edited; ZI and IZ do not tensor together to create ZZ, but rather they simply multiply. I’ve amended the original post.]

---

<div class="post-metadata">

**Author:** ![Vedant\_Dwivedi](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vedant_dwivedi/32/813_2.png) [@Vedant\_Dwivedi](https://discuss.pennylane.ai/u/Vedant_Dwivedi)\
**Post date:** [November 26, 2021, 4:14am UTC](https://discuss.pennylane.ai/t/any-thought-on-i11-4/1510/6 "2021-11-26T04:14:30Z")

</div>

Got it Thanks 👍✌@glassnotes

---

<div class="post-metadata">

**Author:** ![glassnotes](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/glassnotes/32/327_2.png) [@glassnotes](https://discuss.pennylane.ai/u/glassnotes)\
**Post date:** [November 26, 2021, 2:16pm UTC](https://discuss.pennylane.ai/t/any-thought-on-i11-4/1510/7 "2021-11-26T14:16:11Z")

</div>

@Vedant_Dwivedi awesome, glad to hear it!
