<jupyter_start><jupyter_text>Quantum tomography W-checker Check for the hidden state is W state or not?The $U_{gen}$ is generated from n-layers circuit:the $U_{target}$ is constructing W circuit inverse:<jupyter_code>import qiskit, sys
import numpy as np, matplotlib.pyplot as plt
sys.path.insert(1, '../')
import qtm.base, qtm.constant, qtm.ansatz, qtm.custom_gate
# Init parameters
num_qubits = 10 
# For arbitrary initial state
num_layers = 2
thetas = np.ones(num_qubits*num_layers*5)
# For determine W state

qc = qiskit.QuantumCircuit(num_qubits, num_qubits)
thetass, loss_values = qtm.base.fit(
    qc, num_steps = 100, thetas = thetas, 
    create_circuit_func = qtm.ansatz.create_Wchecker_linear, 
    grad_func = qtm.base.grad_loss,
    loss_func = qtm.loss.loss_fubini_study,
    optimizer = qtm.optimizer.adam,
    verbose = 1,
    num_layers = num_layers,
    is_return_all_thetas = True
)
np.savetxt("../../experiments/linear_ansatz_w/" + str(num_qubits) + "/loss_values_adam.csv", loss_values, delimiter=",")
np.savetxt("../../experiments/linear_ansatz_w/" + str(num_qubits) + "/thetass_adam.csv", thetass, delimiter=",")
plt.plot(loss_values)
traces, fidelities = [], []
for thetas in thetass:
    # Get |psi> = U_gen|000...>
    qc = qiskit.QuantumCircuit(num_qubits, num_qubits)
    qc = qtm.ansatz.create_linear_state(qc, thetas, num_layers = num_layers)
    psi = qiskit.quantum_info.Statevector.from_instruction(qc)
    rho_psi = qiskit.quantum_info.DensityMatrix(psi)
    # Get |psi~> = U_target|000...>
    qc1 = qiskit.QuantumCircuit(num_qubits, num_qubits)
    qc1 = qtm.ansatz.create_w_state(num_qubits)
    psi_hat = qiskit.quantum_info.Statevector.from_instruction(qc1)
    rho_psi_hat = qiskit.quantum_info.DensityMatrix(psi_hat)
    # Calculate the metrics
    trace, fidelity = qtm.base.get_metrics(psi, psi_hat)
    traces.append(trace)
    fidelities.append(fidelity)
np.savetxt("../../experiments/linear_ansatz_w/" + str(num_qubits) + "/traces_adam.csv", traces, delimiter=",")
np.savetxt("../../experiments/linear_ansatz_w/" + str(num_qubits) + "/fidelities_adam.csv", fidelities, delimiter=",")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1333950903195924, 'fraction_numerical': 0.008337193144974525, 'mean_word_length': 4.74468085106383, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '164132', 'license': 'MIT', 'n_tokens_mistral': 868, 'n_tokens_neox': 810, 'n_words': 144, 'path': 'codes/create_circuit/Wchecker.ipynb', 'repo_name': 'vutuanhai237/QuantumTomographyProject', 'size': '2159'}