harbor run -t openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-3563harbor download openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-3563PSEUDOCODE
FUNCTION to_valid_density_matrix(initial_state_index, num_qubits, qid_shape, dtype) // Preconditions // - 0 <= initial_state_index < 2^num_qubits // - qid_shape must reflect 2 for each qubit, e.g., (2,), (2,2), ... // - dtype specifies output numpy dtype IF initial_state_index < 0 OR initial_state_index >= 2 ** num_qubits THEN RAISE ValueError("Invalid quantum state index") ENDIF
tensor_shape := (2,) repeated (2 * num_qubits) times tensor := zeros(tensor_shape, dtype)
// Compute bitstring representation of the basis index i for the N qubits bitstring := bits_of_k(initial_state_index, num_qubits) // length num_qubits
// Set the diagonal projector |i><i| in tensor form row_coords := bitstring col_coords := bitstring tensor[ row_coords + col_coords ] := 1
RETURN tensor END FUNCTION
FUNCTION density_matrix_kronecker_product(a_tensor, b_tensor) // a_tensor has shape (2,)* (2N) // b_tensor has shape (2,) (2*M) N := (length(shape_of(a_tensor)) / 2) M := (length(shape_of(b_tensor)) / 2)
A := reshape(a_tensor, (2 ** N, 2 ** N)) B := reshape(b_tensor, (2 ** M, 2 ** M)) K := kron(A, B) // numpy Kronecker product
new_shape := (2,) repeated (2 * (N + M)) times K_tensor := reshape(K, new_shape)
RETURN K_tensor END FUNCTION
CLASS DensityMatrixSimulationState // Attributes: // - qubits: list of Cirq qubits // - target_tensor: density-matrix tensor of shape (2,)* (2N) // - available_buffer: list of 3 tensors with same shape/dtype as target_tensor // - qid_shape: (2,)*N // - dtype: numpy dtype // - prng: optional PRNG
CONSTRUCTOR(qubits, initial_state, prng = NULL, dtype = complex64) self.qubits := list(qubits) N := length(self.qubits) self.qid_shape := (2,) repeated N times self.dtype := dtype self.prng := prng
IF initial_state IS scalar 0 OR 1
index := 0 IF initial_state == 0 ELSE (2 ** N - 1)
self.target_tensor := to_valid_density_matrix(index, N, self.qid_shape, dtype)
ELSEIF is_numpy_array(initial_state)
s := shape_of(initial_state)
// Case a: 1D vector length 2**N
IF length(s) == 1 AND s[0] == (2 ** N)
vec := initial_state
self.target_tensor := outer(vec, conjugate(vec)) // density from state vector
// Case b: tensor shape (2,)*N
ELSEIF s == (2,) repeated N times
vec := flatten(initial_state)
self.target_tensor := outer(vec, conjugate(vec))
// Case c: full density matrix (2**N, 2**N)
ELSEIF s == (2 ** N, 2 ** N)
self.target_tensor := reshape(initial_state, (2,) repeated N + (2,) repeated N)
// Case d: density tensor already with shape (2,)* (2N)
ELSEIF s == (2,) repeated (2 * N)
self.target_tensor := initial_state
// Case e: explicit density tensor shape (2,)*N + (2,)*N
ELSEIF s == (2,) repeated N + (2,) repeated N
self.target_tensor := initial_state
ELSE
RAISE ValueError("Invalid quantum state")
ENDIF
ELSE
RAISE ValueError("Invalid quantum state")
ENDIF
self.available_buffer := [ zeros_like(self.target_tensor) for _ in range(3) ]
END CONSTRUCTOR
METHOD copy(self, deep_copy_buffers) new := new DensityMatrixSimulationState() // Copy core metadata new.qubits := copy_of(self.qubits) new.qid_shape := self.qid_shape new.dtype := self.dtype new.prng := self.prng
// Copy target and buffers
new.target_tensor := self.target_tensor.copy()
IF deep_copy_buffers
new.available_buffer := [ buf.copy() for buf in self.available_buffer ]
ELSE
new.available_buffer := self.available_buffer // share same list object
ENDIF
RETURN new
END METHOD
METHOD with_qubits(self, new_qubits) m := length(new_qubits) ground := to_valid_density_matrix(0, m, (2,) repeated m, self.dtype) new_target := density_matrix_kronecker_product(self.target_tensor, ground)
// Build new instance by constructing internal state directly
new := new DensityMatrixSimulationState()
new.qubits := self.qubits + list(new_qubits)
new.qid_shape := (2,) repeated length(new.qubits)
new.dtype := self.dtype
new.prng := self.prng
new.target_tensor := new_target
new.available_buffer := [ zeros_like(new_target) for _ in range(3) ]
RETURN new
END METHOD
// Decomposition support (limited to single-qubit gates) // Helper: apply a single-qubit unitary U on qubit_index (0-based) for N == 1 METHOD _apply_unitary_on_qubit(self, U, qubit_index) N := length(self.qubits) IF N != 1 THEN RAISE NotImplementedError("Decomposition fallback limited to N == 1") ENDIF // ρ is 2x2 Rho := reshape(self.target_tensor, (2,2)) Rho_prime := U * Rho * U_conjugate_transpose // matrix multiply self.target_tensor := reshape(Rho_prime, (2,)*2) END METHOD END CLASS
CLASS _BufferedDensityMatrix STATIC METHOD create(initial_state, qid_shape = NULL, dtype = complex64, ...) IF qid_shape IS NULL RAISE ValueError("qid_shape must be provided") ENDIF // Return a placeholder object; tests only require error path for missing qid_shape RETURN PlaceholderBufferedDensityMatrix() END STATIC METHOD END CLASS
// Error handling helpers (described in pseudocode; implementers to raise exact messages) FUNCTION bits_of_k(k, n) // Return list of n bits representing k in binary, least-significant bit last or as convention used, // ensure consistent with indexing in to_valid_density_matrix // Implementation detail omitted in pseudocode
END FUNCTION
// Notes // - Shape conventions: density tensor shape is (2,)* (2N) // - Ground-state density for new qubits used in with_qubits is the |0...0><0...0| projector // - Exact error messages to satisfy tests: "qid_shape must be provided" and "Invalid quantum state"